模板 class 成员函数的前向声明
Forward declaration of template class member function
这是 Mappings
class 的前向声明:
template<typename Type, typename IDType=typename Type::IDType>
class Mappings;
template<typename Type, typename IDType>
class Mappings
{
public:
...
Type valueFor(const IDType& id);
...
};
如何转发声明 valueFor
函数?
我需要类似的东西
template<typename Type, typename IDType>
Type Mappings::valueFor(const IDType& id)
{
// return value
}
正如上面评论中已经指出的那样,不可能只转发声明 class 的单个成员函数。如果您真正要寻找的是一种在 class:
之外定义成员函数的方法
template <typename Type, typename IDType = typename Type::IDType>
class Mappings;
template <typename Type, typename IDType>
class Mappings
{
public:
Type valueFor(const IDType& id);
};
template <typename Type, typename IDType>
Type Mappings<Type, IDType>::valueFor(const IDType& id)
{
return {};
}
请注意 ::
之前的 class 名称需要包含模板参数。在 class 定义之外的成员函数定义中,名称必须由 class 名称限定 [class.mfct]/4 followed by ::
. Mappings
is the name of a class template, not the name of a class. While, inside the definition of a class template, the name of the template can be used synonymously with the name of the class [temp.local]/1,我们不在任何模板的定义内介绍了这个成员函数的定义。因此,您需要在此处使用 class 的正确名称...
这是 Mappings
class 的前向声明:
template<typename Type, typename IDType=typename Type::IDType>
class Mappings;
template<typename Type, typename IDType>
class Mappings
{
public:
...
Type valueFor(const IDType& id);
...
};
如何转发声明 valueFor
函数?
我需要类似的东西
template<typename Type, typename IDType>
Type Mappings::valueFor(const IDType& id)
{
// return value
}
正如上面评论中已经指出的那样,不可能只转发声明 class 的单个成员函数。如果您真正要寻找的是一种在 class:
之外定义成员函数的方法template <typename Type, typename IDType = typename Type::IDType>
class Mappings;
template <typename Type, typename IDType>
class Mappings
{
public:
Type valueFor(const IDType& id);
};
template <typename Type, typename IDType>
Type Mappings<Type, IDType>::valueFor(const IDType& id)
{
return {};
}
请注意 ::
之前的 class 名称需要包含模板参数。在 class 定义之外的成员函数定义中,名称必须由 class 名称限定 [class.mfct]/4 followed by ::
. Mappings
is the name of a class template, not the name of a class. While, inside the definition of a class template, the name of the template can be used synonymously with the name of the class [temp.local]/1,我们不在任何模板的定义内介绍了这个成员函数的定义。因此,您需要在此处使用 class 的正确名称...