为什么约束函数允许未定义的行为? [英] Why does a consteval function allow undefined behavior?

查看:131
本文介绍了为什么约束函数允许未定义的行为?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

C ++中的常量表达式具有非常整洁的特性:它们的求值不能具有未定义的行为(

There is a very neat property of constant expressions in C++: their evaluation cannot have undefined behavior (7.7.4.7):

表达式e是核心常量表达式,除非按照抽象机([intro.execution])的规则对e求值将对以下值之一进行求值:

An expression e is a core constant expression unless the evaluation of e, following the rules of the abstract machine ([intro.execution]), would evaluate one of the following:

  • ...

  • ...

一种操作,该操作将具有本文档[intro]至[cpp]中指定的未定义行为[注意:例如,包括带符号整数溢出([expr.prop]),某些指针算术([ expr.add]),除以零或某些移位操作-尾注];

an operation that would have undefined behavior as specified in [intro] through [cpp] of this document [ Note: including, for example, signed integer overflow ([expr.prop]), certain pointer arithmetic ([expr.add]), division by zero, or certain shift operations — end note ] ;

尝试将13!的值存储在constexpr int实际上产生一个不错的编译错误:

Trying to store the value of 13! in a constexpr int indeed yields a nice compile error:

constexpr int f(int n) 
{
    int r = n--;
    for (; n > 1; --n) r *= n;
    return r;
}

int main() 
{
    constexpr int x = f(13);
    return x;
}

输出:

9:19: error: constexpr variable 'x' must be initialized by a constant expression
    constexpr int x = f(13);
                  ^   ~~~~~
4:26: note: value 3113510400 is outside the range of representable values of type 'int'
    for (; n > 1; --n) r *= n;
                         ^
9:23: note: in call to 'f(3)'
    constexpr int x = f(13);
                      ^
1 error generated.

(顺便说一句,为什么错误是调用'f(3)'",而它却是对f(13)的调用?.)

(BTW why does the error say "call to 'f(3)'", while it is a call to f(13)?..)

然后,我从x中删除constexpr,但是将f设置为consteval.根据文档:

Then, I remove constexpr from x, but make f a consteval. According to the docs:

consteval-指定一个函数为立即函数,也就是说,每次对该函数的调用都必须产生一个编译时常量

consteval - specifies that a function is an immediate function, that is, every call to the function must produce a compile-time constant

我确实希望这样的程序会再次导致编译错误.但是,相反,该程序可以与UB一起编译并运行.

I do expect that such a program would again cause a compile error. But instead, the program compiles and runs with UB.

那是为什么?

UPD::评论者建议这是一个编译器错误.我报告了它: https://bugs.llvm.org/show_bug.cgi?id=43714

UPD: Commenters suggested that this is a compiler bug. I reported it: https://bugs.llvm.org/show_bug.cgi?id=43714

推荐答案

这是一个编译器错误.或者,更确切地说,这是未实现的"解决方案.功能(请参见 bugzilla中的注释):

This is a compiler bug. Or, to be more precise, this is an "underimplemented" feature (see the comment in bugzilla):

是的-根据: https://clang.llvm,似乎还没有实现. org/cxx_status.html

(可能已添加了关键字,但未添加实际的实现支持)

(the keyword's probably been added but not the actual implementation support)

这篇关于为什么约束函数允许未定义的行为?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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