gpt4 book ai didi

c++ - VC++ SFINAE 给出错误 C2070 : 'overloaded-function' : illegal sizeof operand

转载 作者:塔克拉玛干 更新时间:2023-11-03 06:46:52 26 4
gpt4 key购买 nike

错误的性质与 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 的其他解决方法,但没有成功。此问题是否有任何修复或更好的解决方法?

最佳答案

虽然下面是不正确的答案,但它至少解决了问题。在代码片段中,我更改了 2 行:

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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com