小编Sae*_*nen的帖子

C++ Cast模板

我有这个源代码,允许我投射Point<float>Point<double>:

template<class T> struct Point{
    template <typename NewType> Point<NewType> cast() const{
        return Point<NewType>();
    }       
};

int main(){
    Point<float> p1;
    Point<double> p2;
    p2 = p1.cast<double>();

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

这个源代码编译得很好.现在,我添加以下类,我在执行转换的行中有一个编译错误:

template <class T> struct PointContainer{
    void test(){
        Point<T> p1;
        Point<double> p2;
        p2 = p1.cast<double>(); //compilation error
    }
};
Run Code Online (Sandbox Code Playgroud)

编译错误:error: expected primary-expression before ‘double’.

为什么我会收到此错误,如何解决?

c++ templates

4
推荐指数
3
解决办法
782
查看次数

标签 统计

c++ ×1

templates ×1