c++ - 默认模板参数结果为 "expected type-specifier"

标签 c++ templates

以下案例在 MS Visual Studio 中编译良好,但在 g++ 4.6 中编译失败。

编译:

    template <typename T>
    struct get_type
    { typedef void type_;};

template <>
    struct get_type<float>
    { typedef float type_; };

template <>
    struct get_type<int>
    { typedef int type_; };

template <typename T, typename P=get_type<T>::type_> // <--- line 16
    struct get_destroy_type
    { static inline void exec(P a) {} };

结果:

../testlibrary/testlibrary.h:16:34: error: expected type-specifier
../testlibrary/testlibrary.h:16:34: error: expected ‘>’

我用的时候好像不太喜欢

get_type<T>::type_

作为模板参数默认值。 MS Visual Studio (Express 10) 可以很好地编译它。我可以做哪些更改来让 g++ 编译它?

最佳答案

使用typename 来消除歧义:

template <typename T, typename P = typename get_type<T>::type_>
                                   |______|

关于c++ - 默认模板参数结果为 "expected type-specifier",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13184142/

相关文章:

c++ - 为什么 `boost::lower_bound` 按值接受它的参数?

c++ - 仅使用前 3 位小数计算,如何?? (c++)

c++ - 在 Eclipse ADT 中,我创建一个新的 cpp 文件并收到 "undefined reference"错误

c# - Visual Studio调试器在混合 Debug模式下停止击中断点

c++ - -> 之后带有类型别名的推导指南

python - C++ 和 cython - 寻求一种避免模板限制的设计模式

c++ - 平台物理弹跳故障 C++

c++ - 编写模板

c++ - 内联模板函数

c++ - 使用 Pair 模板实现 List 模板