There is such a code:
template<typename T> struct somestruct { ... T somefunction(somestruct<T>* const th = this) { return th ? th->somefield1 : this->somefield2; } ... }; Compiler VS 2017 swears:
Error C2355 "this": can only be specified in non-static member functions or initializers of non-static data members
Please explain why I cannot use this in a similar way.
I would like to call somepointer->somefunction(otherpointer) return the pointer object field from the argument if it is not nullptr , otherwise return another field of the pointer object that called the method. And if somefunction is called without arguments, then it would be similar to calling somepointer->somefunction(somepointer) . Of course, you can make such a call explicitly or simply overload somefunction , but I wonder why it is wrong as I wrote.