I am trying to inherit a struct in order to add a += operator.
This code seems right, but it fails to compile, and all I get from the compiler is this:
syntax error : missing ',' before '<'
see reference to class template instantiation 'Test' being compiled
struct plus_equals
{
template<typename T, typename S>
struct operator_type
{
S& operator+=(const T &other)
{
S* super = (S*)this;
super->value += other;
return *super;
}
};
};
template<typename T, typename OP>
struct Test : OP::operator_type<T, Test<T, OP>> // error on this line
{
T value;
Test(T val) : value(val){}
};
int main(int argc, char *argv[])
{
Test<int, plus_equals> test(123);
test += 456;
cout << test.value << endl;
return 0;
}
I'm confused why the code below will compile, but the above will not.
template<typename T>
struct Something
{
T GetT() { return T(); }
};
class Test : public Something<Test>
{
//...
};