模板化类的 C++ 转换

Ant*_*ier 2 c++ gcc templates casting

我有一个关于模板的问题。我想要一个模板化的类,它包含一个浮点数或双精度数的数组。

我可以编写一个复制它的 clone() 函数。没问题。但是,我想要另一个名为 cast() 的函数,它可以在 double 和 float 之间来回转换。之前已经讨论过这个问题,但我认为问题不一样:

stackoverflow.com/questions/714213/c-template-casting

我遇到的问题是编译器错误,而不是链接器错误。错误信息是:

main.cpp: In function `void caster()':
main.cpp:63: error: expected primary-expression before "double"
main.cpp:63: error: expected `;' before "double"
main.cpp:64: error: expected primary-expression before "float"
main.cpp:64: error: expected `;' before "float"
main.cpp:65: error: expected primary-expression before '>' token
main.cpp:65: error: expected primary-expression before ')' token
Run Code Online (Sandbox Code Playgroud)

我转储了下面的代码。第 63、64 和 65 行是我用“此处出错”注释的地方。

顺便说一下,我的编译器是“GNU C++ version 3.4.5 20051201 (Red Hat 3.4.5-2) (x86_64-redhat-linux) 由 GNU C version 3.4.5 20051201 (Red Hat 3.4.5-2) 编译” .

经过一番谷歌搜索,发现有人已经遇到过这个问题:

gcc.gnu.org/ml/gcc-help/2006-04/msg00022.html

这里有一个解决方案:

gcc.gnu.org/ml/gcc-help/2006-04/msg00023.html

但是当原始海报询问它为什么起作用时,答案不是很清楚:

gcc.gnu.org/ml/gcc-help/2006-04/msg00025.html

不幸的是,链接已经失效,我没有第三版的 Stroustrup。现在,我有我的修复,我的代码有效。但是,Stackoverflow,它为什么有效?


#include <stdio.h>

// =================== This would be the header ===================
template <class T>
class foo
{
public:
             foo(const T val) {d_data = new double; *d_data = val;}
    virtual ~foo() {delete d_data;};

    foo* clone() const;

    template<class U>
    foo<U>* cast() const;

private:
    double *d_data;
};

// =================== This would be the implementation of the class ===================
template<class T>
foo<T>* foo<T>::clone() const
{
    return new foo<T>(*d_data);
}

template<class T>
template<class U>
foo<U>* foo<T>::cast() const
{
    return new foo<U>(*d_data);
}

template class foo<float>;
template class foo<double>;

template foo<float>* foo<float>::cast() const;
template foo<float>* foo<double>::cast() const;
template foo<double>* foo<float>::cast() const;
template foo<double>* foo<double>::cast() const;

// =================== Using the class ===================
template <class T>
void caster()
{
    foo<double> *f1 = NULL;
    foo<float>  *f2 = NULL;
    foo<T>      *f3 = NULL;

    // I am looking at something that compiles
    // I don't care about linking for now
    // This will crash at runtime because of
    // NULL, but that's just an example

    f1->cast<double>(); // compiler OK
    f1->cast<float>();  // compiler OK
    f1->cast<T>();      // compiler OK

    f2->cast<double>(); // compiler OK
    f2->cast<float>();  // compiler OK
    f2->cast<T>();      // compiler OK

    f3->cast<double>(); // Error here
    f3->cast<float>();  // Error here
    f3->cast<T>();      // Error here

    f3->foo<T>::template cast<double>(); // It works!
    f3->foo<T>::template cast<float>();  // It works!
    f3->foo<T>::template cast<T>();      // It works!
}

int main(int argc, char **argv)
{
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

Chu*_*dad 5

f3->cast<double>(); // Error here 
Run Code Online (Sandbox Code Playgroud)

在这一行中,编译器不知道<afterf3->cast是表示模板参数的开始还是小于比较运算符。

明确指定它代表模板参数的开始。正确的方法是

f3->template cast<double>();
Run Code Online (Sandbox Code Playgroud)

总之,.template符号(以及类似的符号如->template)只应使用模板内且仅当他们遵循的东西,取决于模板参数(如表达f3这取决于模板参数T