-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.

Lucas Hoof
  • 15
  • 4
  • 1
    `public static void IncVec(ref Vec3 vec)` – Johnathan Barclay May 22 '20 at 08: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. – Damien_The_Unbeliever May 22 '20 at 08: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? .. – Lucas Hoof May 22 '20 at 08:44
  • @Damien_The_Unbeliever i'll keep that in mind. I just started playing around with c# .. thanks for the info. – Lucas Hoof May 22 '20 at 08:45
  • 1
    They considered adding an [`IArithmetic`](https://referencesource.microsoft.com/#mscorlib/system/int32.cs,249) at one point but it's never happened. So you can't do maths with generic types. – Damien_The_Unbeliever May 22 '20 at 08:47
  • the problem you face related to Generics in C#. If you try to apply an operator (like +) between two generic types it should be possible to use by any type passed to T within constraints. In this case, as @Damien_The_Unbeliever mentioned there is no constraint that specifies only arithmetic types. You need some workarounds like [this](https://stackoverflow.com/questions/9338398/how-to-sum-generic-numbers-in-c) – Eldar May 22 '20 at 08:48
  • Is there any reason you specifically want a `struct`? Modifying structs is often a bad thing. [They should be immutbale](https://stackoverflow.com/questions/441309/why-are-mutable-structs-evil). Using a class lets you modify any member directly without the need for `ref`. Alternativly create a new instance of the struct, instead of modifying an existing one. – MakePeaceGreatAgain May 22 '20 at 08:55
  • There wasnt really any reason to use struct, as I said I was just trying to get to know c# better.. just playing around with it.. (in this case with ref) i wasnt actually aware of it being way more different from class.. anyway, thanks for advice. Have a nice day! – Lucas Hoof May 22 '20 at 09:01

1 Answers1

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.

Mattias Larsson
  • 763
  • 6
  • 15