退货类型中不需要模板参数

No Template Parameter Needed in Return Type

本文关键字:参数 不需要 类型      更新时间:2023-10-16
template <typename Type>
class Foo
{
    Foo& Bar()
    {
        return *this;
    }
};

为什么要编译?我不应该在返回类型中指定模板参数吗?

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

Foo<Type>是隐含的,因为Bar的定义在类的定义中。如果它在类定义之外,那么你必须明确地定义它:

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