Consider the example below:
template <typename T>
class SomeClass {
// rest of the definition ...
SomeClass<T>& function1() {
// ...
return *this;
}
SomeClass& function2() {
// ...
return *this;
}
}
Is there a difference between return values of the two functions above? If not, which one should be preferred?
There is no difference between
function1
andfunction2
. TypingSomeClass
without template arguments in the definition ofSomeClass
is an instance of using an injected-class-name:It is subjective whether to use one over the other, but the injected class name is more maintainable as it doesn't have to be manually changed if you change the number of template parameters of
SomeClass
.