错误的性质与this thread中提到的不同。 .
我正在尝试在 VC++ 环境中实现 SFINAE 方式,该方式查找类是否包含成员(方法)。
下面是一个简化的代码:
template<typename Class>
class HasMember_Method
{
typedef char (&yes)[2];
template<unsigned int> struct Exists;
template<typename V>
static yes CheckMember (Exists<sizeof(&V::Method)>*); // <--- VC++ problem
template<typename>
static char CheckMember (...);
public:
static const bool value = (sizeof(CheckMember<Class>(0)) == sizeof(yes));
};
这里的Method
就是我们要寻找的成员方法。这段代码在 g++ environment 中运行得很好即使没有 C++11。
然而,对于有缺陷的 VC++,同样会导致编译器错误:
error C2070: 'overloaded-function': illegal sizeof operand
我使用 decltype
尝试了 SFINAE 的其他解决方法,但没有成功。对于这个问题是否有任何修复或更好的解决方法?
最佳答案
虽然下面的答案不正确,但至少解决了问题。在代码片段中,我更改了两行:
template<typename Class>
class HasMember_Method
{
typedef char (&yes)[2];
template<typename> struct Exists; // <--- changed
template<typename V>
static yes CheckMember (Exists<decltype(&V::Method)>*); // <--- changed (c++11)
template<typename>
static char CheckMember (...);
public:
static const bool value = (sizeof(CheckMember<Class>(0)) == sizeof(yes));
};
这解决了 VS2010/12 中的编译问题!
等等!这揭示了有缺陷的 VC++ 编译器中的另一个缺陷。
结果总是正确的。 :(
good old g++ works这里也很好。
所以正确的答案是等待 Microsoft VC++ 编译器团队修复这些问题。
关于c++ - VC++ SFINAE 给出错误 C2070 : 'overloaded-function' : illegal sizeof operand,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20971749/