1

I am attempting to implement the generic Vector3 struct, and havethe operators for my struct that allows basic math when the type T is a numeric (int, float, double, long, short)

I had thought the way to do this was to just define the 4 basic operators for all each something like

public static Vector3<int> operator +(Vector3<int> left, Vector3<int> right) but that gives me the error that at least one of the parameters must be of the containing type (which is Vector3 in this case)

I feel reasonably confident there is a way for me to define a Vector3 generic, and still have the convenience of the standard operators, but I can not seem to figure out what I need to write syntactically.

Azeranth
  • 151
  • 11
  • public struct Vector3 { T x; T y; T z; } – Azeranth Aug 27 '19 at 22:05
  • I am having trouble finding a `Vector3` class. Is this something you wrote? – John Wu Aug 27 '19 at 22:16
  • @Azeranth please update your question with the struct you're creating, along with the operator overload that's not working (code in comments isn't as helpful). – Rufus L Aug 27 '19 at 22:18
  • @Rufus That link is for `Vector3`, not `Vector3`. There is also a `Vector`. But there is no `Vector3` I'm aware of. – John Wu Aug 27 '19 at 22:18
  • Maybe [this question](https://stackoverflow.com/questions/3598341/define-a-generic-that-implements-the-operator) has an answer that will help here? – Rufus L Aug 27 '19 at 22:26

1 Answers1

0

I believe you are trying to do something like this:

public class Vector3<T>
{ 
    T x; T y; T z; 

    public static Vector3<int> operator + (Vector3<int> lhs, Vector3<int> rhs)
    {
        //Stuff
    }
}

This is not allowed. Why not? Well imagine you wrote a method like this:

public static void Foo<T>()
{
    var lhs = new Vector3<T>();
    var rhs = new Vector3<T>();

    var result = lhs + rhs;
}

Should the compiler allow this to compile or not? Because this would work:

Foo<int>();

But this would fail:

Foo<string>();

Because the compiler can't guarantee it'll work, it's not allowed.

If you have a burning desire to implement operator overloading for certain types of Vector3, you have to subclass it:

public class Vector3Int : Vector3<int>
{
    public static Vector3Int operator + (Vector3Int lhs, Vector3Int rhs)
    {
        //Stuff
    }
}

That would work. Note that I had to change struct to class as you can't inherit a struct.

John Wu
  • 50,556
  • 8
  • 44
  • 80
  • But that piece of erroneous code should tell me "Unable to apply operator '+' to types "Vector3" and "Vector3". Requiring that I only use the operator when the type is known or given explicitly – Azeranth Aug 27 '19 at 22:54
  • How could the compiler possibly know what `T` will be passed into a call to `Foo`? Bearing in mind that this code could be compiled into an assembly and called from a program for which you don't even have source code. – John Wu Aug 27 '19 at 23:00
  • Well it wouldn't, hence the error, since the explicit type is T. But I should be able to declare a List> Foo; and then say Foo[0] += Foo[1] – Azeranth Aug 27 '19 at 23:03
  • That's not how generics work. A generic has to handle any type of `T`, subject to [type constraints](https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/generics/constraints-on-type-parameters). If you want to pick and choose specific types, it's not generic-- you can use traditional polymorphism or some other programming construct, but not generics. – John Wu Aug 27 '19 at 23:05