Maxpm
Maxpm

Reputation: 25551

No Template Parameter Needed in Return Type

template <typename Type>
class Foo
{
    Foo& Bar()
    {
        return *this;
    }
};

Why does this compile? Shouldn't I have to specify the template parameter in the return type?

Foo<Type>& Bar()
{
     return *this;
}

Upvotes: 2

Views: 142

Answers (1)

fbrereto
fbrereto

Reputation: 35925

Foo<Type> is implied because the definition of Bar is within the definition of the class. If it were outside the class definition then you'd have to define it explicitly:

template <typename Type>
Foo<Type>& Foo<Type>::Bar()
{
    return *this;
}

Upvotes: 2

Related Questions