在constexpr函数中声明 [英] assert in constexpr function

查看:118
本文介绍了在constexpr函数中声明的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在试图弄清为什么出现某个编译错误的原因时,我想到了以下最小示例:

In trying to work out why I was getting a certain compile error, I came up with the following minimal example:

constexpr void Test(bool test)
{
    if (test)
        return;

    assert(false);
}

我尝试过的每个版本的clang(3.7+)都可以正常编译,但由于gcc失败(测试5-8),所以

This compiles without issue with every version of clang I tried (3.7+), but fails with gcc (tested 5-8), with


错误:调用非'constexpr'函数'void __assert_fail(const
char *,const char *,unsigned int,const char *)'

error: call to non-‘constexpr’ function ‘void __assert_fail(const char*, const char*, unsigned int, const char*)’

根据我的理解,该功能应该能够成为 constexpr ,因为有一组参数值可以在编译时对其进行求值。

From my understanding, the function should be able to be constexpr because there is a set of argument values for which the function can be evaluated at compile time.

我的理解是错误的,还是gcc无法编译此错误?

Is my understanding wrong, or is gcc incorrect in failing to compile this?

推荐答案

这是 GCC错误86678 ,并已在几天前修复。

This is GCC bug 86678, and was fixed just a few days ago.

这篇关于在constexpr函数中声明的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆