Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8503713
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T01:49:53+00:00 2026-06-11T01:49:53+00:00

I want to have 2d vector classes for every primitive type. Right now, to

  • 0

I want to have 2d vector classes for every primitive type.

Right now, to assure the best runtime performance and be able to use many utility functions, I need to have a separate class for every primitive (Vector2Int, Vector2Float, Vector2Long, etc).

It’s just a lot of copy-pasting, and if I have to make a change I have to remember to do it in every class and in every utility function.

Is there anything that lets me write something like C++ templates (or is there any way I can create it)?

I created a little concept to show you how this would work:

// compile is a keyword I just invented for compile-time generics/templates

class Vector2<T> compile T : int, float, double, long, string
{
    public T X { get; set; }
    public T Y { get; set; }

    public T GetLength() 
    {
        return Math.Sqrt(Math.Pow(X, 2) + Math.Pow(Y, 2));
    }
}

// during compilation, code will be automatically generated
// as if someone manually replaced T with the types specified after "compile T : "
/*
    VALID EXAMPLE (no compilation errors):

    autogenerated class Vector2<int>
    {
        public int X { get; set; }
        public int Y { get; set; }

        public int GetLength() 
        {
            return Math.Sqrt(Math.Pow(X, 2) + Math.Pow(Y, 2));
        }
    }



    UNVALID EXAMPLE (build failed, compilation errors):

    autogenerated class Vector2<string>
    {
        public string { get; set; } // ok
        public string { get; set; } // ok

        public string GetLength() 
        {
            return Math.Sqrt(Math.Pow(X, 2) + Math.Pow(Y, 2)); // error! string cannot be used with Math.Pow()
                                             // and Math.Sqrt doesn't accept string type
        }
    }
*/

Is there some clever way to implement this, or is this completely impossible?


Sorry for not being very clear, but let me explain what the problem is.

Consider using normal C# generics. The GetLength() method wouldn’t compile, because all the types I want to use (int, float, double, long) would require to share an interface which Math.Pow() should accept as a parameter.

Literally substituting the “T” token with the type names would solve this problem, increase flexibility, reach hand-written code performance and speed up development.


I made my own template generator, which generates C# code by writing C# code 🙂
http://www.youtube.com/watch?v=Uz868MuVvTY

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T01:49:55+00:00Added an answer on June 11, 2026 at 1:49 am

    Unfortunately, generics in C# are very different than templates in C++. In order to accomplish this, a shared interface (such as IArithmetic) would have to exist (which has been highly requested, but not implemented)* for the different types, and this doesn’t in the framework now.

    This can be done via code generation and T4 templates, however, but it requires generating the code for each type based off a shared “template”.

    *Note: The connect request appears to be blocked, at least temporarily.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a vector that I want to use to create a heap. I'm
I have 2 classes and one of them has map<string, vector<string> > I want
I have vector< pair<int, int>> myVec (N); I want to have all pairs initialized
I have a Vector receiptOrder and I want to print that receipt to thermal
I have a vector of beans that holds information I want to display in
I have a vector X that contains positive numbers that I want to bin/discretize.
I have a vector for example a = [0 1 0 3] I want
I have the vector d<-1:100 I want to sample k=3 times from this vector
I have a vector/array of n elements. I want to choose m elements. The
Say I have a vector: (def data [Hello World Test This]) And I want

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.