尝试编译SFINAE检查中使用的方法体时发生编译错误
Compilation error when trying to compile body of a method used in SFINAE checks
我有一个众所周知的类示例,它可以检测类型是否为类。
#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("%dn", ClassDetector<int>::Yes);
return 0;
}
代码不在GCC
和Clang
上编译,因为"编译错误"在任何地方都没有定义,编译器也不知道它是什么。但它在Visual Studio
下工作。这表明Visual Studio完全跳过了第一个test
方法的主体,甚至不检查其中的任何内容。
所以,问题是:在这种情况下,谁是对的?GCC/Clang还是VS?是否应该检查该方法的编译错误,或者这不是必要的,也不是标准强制执行的?
您很可能是在VS中使用允许模式构建的,该模式禁用了两阶段查找(最近已经实现(。通过指定/permissive-
参数禁用许可模式也会导致VS中的编译失败。
相关文章:
- 有没有一种方法可以在编译时获得作用域类名
- 访问条件类成员的方法不仅在被调用时才编译
- 模板方法访问正向声明的类仅在没有此指针的情况下无法编译
- C++11:模板方法的模板函数调用无法编译?
- 在编译时评估函数开销的通用方法
- VS2017 版本 15.8.3 成功编译内联方法,而不返回所需值
- 在C++中执行 N 阶乘编译时间的 3 种不同/相同方法
- unordered_set .find 方法无法编译的 C++ "expression must have class type"错误
- 如何检查在编译时是否调用了模板化方法?
- C++基于1而不是0的访问数组,方法是在编译时移位
- std::value templated 方法的函数使用 clang 和 g++ 进行编译,但不使用 msvc 进行编译
- CMake + Qt,moc 编译失败,无法实现 QMetaObject 方法(编译器找不到基本 ui 对象的标头?
- 返回实例变量的c++方法可以访问变量中的数据,但不能更改它,但在编译时不会生成错误
- 尝试编译SFINAE检查中使用的方法体时发生编译错误
- 编译错误 std::vector<std::shared_ptr<T>>迭代器和擦除方法
- C++11 - 获取编译时的所有类变量,并在没有 Boost 的情况下为它们调用方法
- 模板代理方法无法编译
- C ++中的方法覆盖:是编译时还是运行时多态性?
- 替换枚举以最大化编译时间检查的最佳方法
- AOT问题,消息"正在Xamarin iOS上尝试使用Pinvoke JIT编译方法"