vladon vladon - 2 months ago 10
C++ Question

Return class ref from class template function

Given a class template:

template <typename T>
class my_class
{
public:
my_class& test1() { return *this; }
// OR
my_class<T>& test2() { return *this; }
}


Is there any difference between return types of
test1
and
test2
?

Answer

Is there any difference between return types of test1 and test2?

No. There is a concept called injected-class-name. Within the body of my_class<T>, the name my_class refers to the full type my_class<T>.

We can even take this to its logical conclusion and add:

my_class::my_class::my_class::my_class& test4() { return *this; }
Comments