尝试编译SFINAE检查中使用的方法主体时出现编译错误

我有一个众所周知的类示例,用于检测类型是否为类。

#include <cstdio>
#include <type_traits>

template <typename T>                                        
class ClassDetector {
private:
    typedef char One;
    typedef struct { char a[2]; } Two;

    template <typename C> static One test(int C::*) {
        compilation error;
    }

    template <typename C> static Two test(...);

public:
    enum { Yes = sizeof(ClassDetector<T>::test<T>(0)) == 1 };
    enum { No = !Yes };
};

int main() {
    printf("%d\n",ClassDetector<int>::Yes);
    return 0;
}

代码无法在GCCClang上编译,因为“编译错误”未在任何地方定义,并且编译器也不知道它是什么。但是它在Visual Studio下有效。这表明Visual Studio完全跳过了第一个test方法的主体,甚至不检查其中的任何内容。

因此,问题是:在这种情况下谁是对的?是GCC / C语还是VS?应该检查该方法是否存在编译错误,或者该方法不是必需的,也不是标准不强制执行的?

kyony 回答:尝试编译SFINAE检查中使用的方法主体时出现编译错误

您很有可能在VS中使用许可模式进行构建,该模式会禁用两阶段查找(最近已实现)。 Disabling permissive mode by specifying /permissive- argument will cause compilation in VS to fail as well

本文链接:https://www.f2er.com/3165983.html

大家都在问