-1

I've been trying to get on top of this for some time now and just cant figure this out.

#include <iostream> template<class T> struct Vec3 { T x, y, z; Vec3(T _x, T _y, T _z) : x(_x), y(_y), z(_z) {} }; template <class T> void IncVec(Vec3<T>& vec) { vec.x += 1; } int main(void) { Vec3<float> vec = Vec3<float>(1, 2, 3); IncVec(vec); std::cout << vec.x; } 

I basically want to recreate this in C#. So far I've got this:

using System; namespace cs_playground { public struct Vec3<T> { public T x, y, z; public Vec3(T _x, T _y, T _z) { x = _x; y = _y; z = _z; } } class Program { public static void IncVec(ref Vec3<T> vec) { vec.x += 1; } static void Main() { Vec3<float> vec = new Vec3<float>(1, 2, 3); IncVec(ref vec); Console.WriteLine(vec.x); } } } 

It really doesnt like the "ref Vec3< T >" .. I mean.. it obviously works with like "ref Vec3< float >" and so on.. but I struggle with like.. passing the type I guess..

Thanks for any kind of help in advance.

8
  • 1
    public static void IncVec<T>(ref Vec3<T> vec) Commented May 22, 2020 at 8:39
  • 1
    In general, don't try to treat C# and C++ as the same things. Generics aren't Templates. structs and classes mean different things in the two languages, etc. Commented May 22, 2020 at 8:39
  • @JohnathanBarclay i tried that, but it wont let me use + operator for int + struct.. cant I use something, where I could do something like (ref vec... ) where T : numeric or smth? .. Commented May 22, 2020 at 8:44
  • @Damien_The_Unbeliever i'll keep that in mind. I just started playing around with c# .. thanks for the info. Commented May 22, 2020 at 8:45
  • 1
    They considered adding an IArithmetic at one point but it's never happened. So you can't do maths with generic types. Commented May 22, 2020 at 8:47

1 Answer 1

2

The main problem would be the vec.x += 1 part. The IncVec should be able to take any type of T and expect it to be able to add 1 to it. But what if T is a bool? You cannot add 1 to a bool in C#. Or a random class?

As @Damien_The_Unbeliever pointed out a Template in C++ is not the same thing as Generics in C#.

However, in C# you can add constrains to the type T like the following (also notice your missing <T> after the method name):

public static void IncVec<T>(ref Vec3<T> vec) where T : xxxx // Constraints to the generic type { vec.x += 1; } 

where xxxx could for instance be the name of a type or interface (which means that the type T must inherit or implement this type). It could also be class to say that the type T must be a class (and then e.g. int will not be allowed) or it could be struct to prevent classes to be used.

But there is no constraint you can specify that allow you to use the code += 1 that I can think of.

Sign up to request clarification or add additional context in comments.

1 Comment

no possible constraint as C# currently stands since operators are static and so cannot form part of an interface.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.