以下案例在 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/