c – 在Clang vs. GCC与MSVC中的SFINAE和能见度检查 – 这是正确的吗?

c – 在Clang vs. GCC与MSVC中的SFINAE和能见度检查 – 这是正确的吗?,第1张

概述我已经尝试了与is_default_constructible的C 03兼容的实现: template<class = void> struct is_default_constructible;template<> struct is_default_constructible<>{protected: // Put base typedefs here to avoid poll 我已经尝试了与is_default_constructible的C 03兼容的实现:
template<class = voID> struct is_default_constructible;template<> struct is_default_constructible<>{protected:    // Put base typedefs here to avoID pollution    struct twoc { char a,b; };    template<bool> struct test { typedef char type; };    template<class T> static T declval();};template<> struct is_default_constructible<>::test<true> { typedef twoc type; };template<class T> struct is_default_constructible : is_default_constructible<>{private:    template<class U> static typename test<!!sizeof(::new U())>::type sfinae(U*);    template<class U> static char sfinae(...);public:    static bool const value = sizeof(sfinae<T>(0)) > 1;};

当我在GCC中测试它(-std = c 03)时,它返回0,因为构造函数是不可见的:

class Test { test(); };int main(){    return is_default_constructible<Test>::value;}

当我在Visual C中测试它时(不同的版本都有相同的行为),我回来1.

当我在Clang中测试它(也是-std = c 03)时,我得到:

error: calling a private constructor of class 'Test'template<class U> static typename test<!!sizeof(::new U())>::type sfinae(U *);                                                      ^note: while substituting explicitly-specifIEd template arguments into function template 'sfinae' static bool const value = sizeof(sfinae<T>(0)) > 1;                                 ^note: in instantiation of template class 'is_default_constructible<Test>' requested herereturn is_default_constructible<Test>::value;       ^error: calling a private constructor of class 'Test'template<class U> static typename test<!!sizeof(::new U())>::type sfinae(U *);                                                      ^note: while substituting deduced template arguments into function template 'sfinae' [with U = Test]static bool const value = sizeof(sfinae<T>(0)) > 1;                                 ^note: in instantiation of template class 'is_default_constructible<Test>' requested herereturn is_default_constructible<Test>::value;

哪个编译器是正确的,为什么?

解决方法 代码无效C 03,虽然它是有效的C 11. g 4.8编译器遵守C 11规则并忽略SFINAE上下文中不可访问的成员,而clang编译器遵守C 03所在的成员(构造函数)找到并选择此案例),但访问检查使代码无效. VS(无论你使用的是什么版本)都不遵守C 11或C 03规则,它似乎完全忽略了sizeof中的访问说明符. 总结

以上是内存溢出为你收集整理的c – 在Clang vs. GCC与MSVC中的SFINAE和能见度检查 – 这是正确的吗?全部内容,希望文章能够帮你解决c – 在Clang vs. GCC与MSVC中的SFINAE和能见度检查 – 这是正确的吗?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/1239623.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-06
下一篇 2022-06-06

发表评论

登录后才能评论

评论列表(0条)

保存