我有一个模板类,它具有需要专门化的模板成员函数,如:
template <typename T>
class X
{
public:
template <typename U>
void Y() {}
template <>
void Y<int>() {}
};
Run Code Online (Sandbox Code Playgroud)
Altough VC正确处理这个,显然这不是标准的,GCC抱怨: explicit specialization in non-namespace scope 'class X<T>'
我试过了:
template <typename T>
class X
{
public:
template <typename U>
void Y() {}
};
template <typename T>
// Also tried `template<>` here
void X<T>::Y<int>() {}
Run Code Online (Sandbox Code Playgroud)
但这导致VC和GCC都抱怨.
这样做的正确方法是什么?
可能重复:
模板:模板功能与类的模板成员函数不兼容
template <typename T>
struct A
{
template <int I>
void f();
};
template <typename T>
void F(A<T> &a)
{
a.f<0>(); // expected primary-expression before ‘)’ token
}
int main()
{
A<int> a;
a.f<0>(); // This one is ok.
}
Run Code Online (Sandbox Code Playgroud)
这是什么一回事?
英特尔C++在编译时是否预先定义了一些宏Qstd=c++0x?像__GXX_EXPERIMENTAL_CXX0X__海湾合作委员会那样的东西 ?__cplusplus还在199711.
有没有办法检测C++ 0x编译?
鉴于:
template <int N>
struct val2size
{
char placeholder[N];
};
Run Code Online (Sandbox Code Playgroud)
有保证sizeof(val2size<N>) == N吗?
我正在尝试为类定义之外的显式专用类模板定义构造函数,如下所示:
template <typename T>
struct x;
template <>
struct x<int> {
inline x();
/* This would have compiled:
x() {
}
*/
};
template <> // Error
x<int>::x() {
}
Run Code Online (Sandbox Code Playgroud)
但这似乎是一个错误.Comeau说:error: "x<int>::x()" is not an entity that can be explicitly specialized尽管完整的课程是专业的.
这是什么问题?
当你进入VS中的调试器并打开反汇编窗口时,每个汇编片段都显示在它的相应代码段下面(或多或少).带-S的GCC仅输出精简组件.
GCC中是否有选项显示原始代码的一些对应关系?
源代码是C++.
GCC似乎不赞成使用本地类实例化模板:
template <typename T>
void f(T);
void g()
{
struct s {};
f(s()); // error: no matching function for call to 'f(g()::s)'
}
Run Code Online (Sandbox Code Playgroud)
VC不抱怨.
应该怎么做?
可能重复:
C++模板可以检查函数是否存在?
我试图确定一个类型有某个成员.这是我试过的:
template <typename T,typename U=void>
class HasX
{
public:
static const bool Result=false;
};
template <typename T>
class HasX<T,typename enable_if_c<(sizeof(&T::X)>0)>::type>
{
public:
static const bool Result=true;
};
struct A
{
int X();
};
struct B
{
int Y();
};
int main()
{
cout<<HasX<A>::Result<<endl; // 1
cout<<HasX<B>::Result<<endl; // 0
}
Run Code Online (Sandbox Code Playgroud)
它实际上是在GCC上编译和工作,但VC error C2070: 'overloaded-function': illegal sizeof operand在实例化时提供.
代码是否有问题,是否有其他方法可以做到这一点?
考虑以下代码:
typedef int type1;
typedef int type2;
template <typename>
struct some_trait;
template <>
struct some_trait<type1>
{
static const int something=1;
};
template <>
struct some_trait<type2>
{
static const int something=2;
};
Run Code Online (Sandbox Code Playgroud)
它失败了,因为编译器看到的是两个特化some_trait<int>.
最好的方法是什么?