目前,我正在发生一些奇怪的事情。我正在尝试使用VC ++编译器编写模板类。

在我的课堂上,为了清楚起见,我得到了一些typedef。我的头文件之外有实际的实现

template<typename T,
    typename = typename std::enable_if<std::is_arithmetic<T>::value,T>::type>
class Integer
{
public:
    typedef  T                      value_type;
    typedef  Integer < value_type > Self;
    typedef  Self&                  SelfReference;
    typedef  Self*                  SelfRawPointer;
    ...
public:
    SelfReference operator =(const SelfReference);


* .tcc-文件:

...
template<typename T> Integer<T>::SelfReference Integer<T>::operator =(const
Integer<T>::SelfReference rhs)
{
    return this->assign(rhs);
};
...


过去使用gcc时,通过这种方式我没有任何问题,但现在在Windows上,编译器抱怨'SelfReference':C2061:语法错误:标识符'SelfReference'

我不知道这是怎么回事,因为过去使用gcc起作用了……我错过了什么吗?如果我内联编写函数,则不会出现该问题。我现在很好奇为什么在Windows上遇到这种问题!

最佳答案

SelfReference是从属类型,因此您需要使用typename

template <typename T>
typename Integer<T>::SelfReference Integer<T>::operator=(const typename Integer<T>::SelfReference rhs) { blah; }


Integer<T>::的右侧,您也可以只使用SelfReference,即。

template <typename T>
typename Integer<T>::SelfReference Integer<T>::operator=(const SelfReference rhs) { blah; }


这是c ++ 11中auto返回值的部分原因,您现在可以编写

template <typename T>
auto Integer<T>::operator=(const SelfReference rhs) -> SelfReference
{
     blah;
}

关于c++ - 带有typedef类型的VC++类模板实现功能,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/43783797/

10-10 08:15