加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c – SFINAE sizeof =检测表达式是否编译

发布时间:2020-12-16 07:51:24 所属栏目:百科 来源:网络整理
导读:我刚刚发现如何检查操作符被提供用于类型. templateclass T T lvalue_of_type();templateclass T T rvalue_of_type();templateclass Tstruct is_printable{ templateclass U static char test(char(*)[sizeof( lvalue_of_typestd::ostream() rvalue_of_typeU
我刚刚发现如何检查操作符<<被提供用于类型.
template<class T> T& lvalue_of_type();
template<class T> T  rvalue_of_type();

template<class T>
struct is_printable
{
    template<class U> static char test(char(*)[sizeof(
        lvalue_of_type<std::ostream>() << rvalue_of_type<U>()
    )]);
    template<class U> static long test(...);

    enum { value = 1 == sizeof test<T>(0) };
    typedef boost::integral_constant<bool,value> type;
};

这个技巧是否知名,还是刚刚获得了编程的诺贝尔奖?

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读