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

c – 使用constexpr C字符串作为编译器错误消息

发布时间:2020-12-16 07:20:59 所属栏目:百科 来源:网络整理
导读:问题表明我想做的是 templateconst char* Errstruct broken{ templatetypename... Args constexpr broken(Args...) { //the sizeof... confuses the compiler as to only emit errors when instantiated //this does not work,static_assert only accepts st
问题表明我想做的是

template<const char* Err>
struct broken
{
    template<typename... Args>
    constexpr broken(Args&&...)
    {
        //the sizeof... confuses the compiler as to only emit errors when instantiated
        //this does not work,static_assert only accepts string literals
        static_assert(sizeof...(Args) < 0,Err);
    }
};

我希望的是,只要实例化,就会发出编译器错误消息Err.但是,static_assert只接受字符串文字作为其第二个参数.有没有办法根据constexpr字符串发出编译器错误?

解决方法

你想要什么不可能以任何形式或形式工作,因为你可以合法地做

extern const char foo[];
template <const char* err> class broken {};
broken<foo> tisbroken;

并且甚至不需要在当前TU(或其他任何地方)中定义foo以进行编译.

当foo未定义时,在foo内部使用err会导致链接器错误,但这样做太迟了.

所以不,你不能使用传递给模板的字符串来打印编译器消息,因为没有字符串.

(编辑:李大同)

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

    推荐文章
      热点阅读