模板<> Solaris CC 需要的语法,但 MSVC 和 GCC 禁止
template<> syntax required by Solaris CC, but forbidden by MSVC and GCC
>我在头文件中有以下代码:
template<typename A, typename B> class TemplateTest;
template<>
class TemplateTest<int, double>
{
public:
float operator() (float a);
};
cpp 文件中的定义:
template<> // this is problematic line
float TemplateTest<int, double>::operator()(float a)
{
float b;
b = a + 5;
return b;
}
使用定义中的"template<>",MSVC 返回错误 C2910,因为它将 operator() 解释为模板方法而不是模板类的方法。 GCC 的行为类似。但是 Solaris CC 需要"template<>"(否则它会发出错误"template<>"语法,当显式专用于...'的成员时,需要语法。
所以我的问题是哪一个是正确的,以及如何使代码在所有这些平台上编译。
Solaris CC 不正确。 不允许template<>
。 C++14 标准 [临时规范]/5:
显式专用类模板的成员的定义方式与普通类的成员相同,并且不使用
template<>
语法。[ 示例:
template<class T> struct A {
struct B { };
template<class U> struct C { };
};
template<> struct A<int> {
void f(int);
};
void h() {
A<int> a;
a.f(16); // A<int>::f must be defined somewhere
}
// template<> not used for a member of an
// explicitly specialized class template
void A<int>::f(int) { /*...*/ }
... - 结束示例 ]
看起来要支持 Solaris CC,您必须使用类似的东西:
#ifdef __SUNPRO_CC
template <>
#endif
float TemplateTest<int, double>::operator()(float a)
{
float b;
b = a + 5;
return b;
}
如果您有很多这样的样板,您可能希望将该样板放入自定义宏中。
你不需要
template<> // this is problematic line
完整示例:
template<typename A, typename B> class TemplateTest;
template<>
class TemplateTest<int, double>
{
public:
float operator() (float a);
};
float TemplateTest<int, double>::operator()(float a)
{
return 0;
}
魔杖盒
在C++参考中查找"专业成员"
相关文章:
- 为什么 gcc 编译这个而 msvc 没有
- CRTP 中的复制赋值运算符 - gcc vs clang 和 msvc
- 命名空间范围内的外部 - GCC vs clang vs msvc
- Visual C++: MSVC vs. GCC+CLANG: 处理 lambda 捕获类成员变量,正确的方法是什么?
- GCC、CLANG 和 MSVC 的可视化C++自动矢量化要求
- 为什么 std::count 比 MSVC 编译器的普通循环慢,但与 GCC 相等?
- 没有'str'成员在 GCC 和 Clang 'std::basic_ostream<char>',但 MSVC 没有问题
- constexpr 函数的常量引用参数:gcc/msvc vs clang/icc
- gcc '-rdynamic' 标志等效于 MSVC
- 为什么MSVC(Visual C++)需要单独的dllimport和dllexport属性,而gcc不需要
- MSVC对这段代码很满意,但GCC并不那么热衷
- 私有运营商删除会触发 GCC 和 Clang 的编译时错误,但不会在 MSVC 上触发编译时错误
- GCC 相当于 MSVC 中的 /GS、/GL、/Gy、/Oi、/MD
- variadic模板代码中的GCC VS MSVC编译误差
- C++指向成员的指针的类内初始化会使 MSVC 失败(但 GCC/Clang 工作)
- GCC 和 clang 抛出"no matching function call"但 msvc (cl) 按预期编译和工作
- enable_if is_same constexpr函数使MSVC失败(但在Clang,GCC中效果很好)
- 在MSVC和GCC上使用VSNPRINTF时不同的行为
- C++ MSVC/GCC/Clang编译器错误
- GCC,Apple LLVM和MSVC编译器的不同部分的名称是什么?