如何编译时断言没有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,这将失败 当我使用static_assert时,他明确告诉我 解决方法
在你的情况下
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 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |