vladon
vladon

Reputation: 8401

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?

Upvotes: 1

Views: 40

Answers (2)

Barry
Barry

Reputation: 302643

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; }

Upvotes: 7

aschepler
aschepler

Reputation: 72271

No, within the scope my_class<T>, my_class is an abbreviation for my_class<T>.

Upvotes: 2

Related Questions