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

如何编译时断言没有C 11

发布时间:2020-12-16 05:42:49 所属栏目:百科 来源:网络整理
导读:在求职面试中,我被要求写一个元功能 确定一个类型是一个指针.这是 我提出了什么 template typename Tstruct is_pointer{ static const bool value = false; }template typename Tstruct is_pointerT *{ static const bool value = true; } 然后我被要求写一
在求职面试中,我被要求写一个元功能
确定一个类型是一个指针.这是
我提出了什么
template <typename T>
struct is_pointer
{ static const bool value = false; }

template <typename T>
struct is_pointer<T *>
{ static const bool value = true; }

然后我被要求写一个meta-assert,这将失败
在编译期间如果我的is_pointer函数不是
做正确的事情

当我使用static_assert时,他明确告诉我
我只能使用C98标准.我该如何实现?

解决方法

在你的情况下
template <bool> struct assert;
template <> struct assert<true> {};

会解决这个问题:

assert<!is_pointer<char>::value>();     // valid
assert<is_pointer<char *>::value>();    // valid

assert<is_pointer<char>::value>();      // compilation error:
                                        // use of incomplete class

(编辑:李大同)

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

    推荐文章
      热点阅读