因此,通常 constexpr 函数是在编译时执行的函数,当传递给它的参数也是 constexpr 时,如下所示:
constexpr int function(int x, int y){
return x+y;
}
参数声明如下:
constexpr int x = 5;
constexpr int y = 6;
将在编译时执行,但带有以下参数声明:
int x=5;
int y=6;
不会的。我想知道如果我们通过以下方式调用这个函数会发生什么:
function(5,6);
从技术角度来看,5和6是右值,但没有办法(我猜)它们可以被转换为constexpr(如果我们可以一般性地说关于转换为constexpr),所以在我看来它将被执行在运行时。然而,没有实际理由在运行时执行它,因为 x 和 y 在编译时都是已知的。
所以我的问题是现实生活中情况如何?这个函数是在运行时还是编译时执行
最佳答案
constexpr int fun(int x, int y) { return x+y; }
fun(5,6) // << constant expression?
tl;博士
5
和 6
是常量表达式。因此,fun(5,6)
也是一个常量表达式,并且将在编译时进行计算,这是强制的(例如非类型模板)。
东西... 我快速浏览了该标准,希望没有遗漏任何重要的要点。
我们已经从@42的回答中得知:
根据 N4527
int
是 constexpr 函数的有效参数类型,因为它是文字类型(因为它是标量类型,由 § 3.9/10 的同一文档是文字类型)。因此,fun
是一个有效的 constexpr 函数。它提供的代码将
fun(5,6)
放入需要常量表达式的上下文中,并且它似乎被某些编译器接受。
现在的问题是这是否有效、符合标准的行为。
N4527 的§5.20 说:
A conditional-expression e is a core constant expression unless the evaluation of e, following the rules of the abstract machine (1.9), would evaluate one of the following expressions:
- here comes a large list of things that prevent expressions from being core constant expression
该列表不包含“带有常量表达式参数的 constexpr 函数”,因此它们是核心常量表达式(除非它们在使用时未定义)。
因此,如果 5
和 6
是常量表达式,则 fun(5,6)
是常量表达式,如果 fun
是一个有效的 constexpr 函数,并且在使用之前定义。给定函数满足第 7.1.5/3 节中所需的约束,并且是有效的 constexpr 函数。
5
和 6
都是 int
类型的整数字面量(根据 §2.13.2)
1) An integer literal is a sequence of digits that has no period or exponent part, with optional separating single quotes that are ignored when determining its value. [...]
2) The type of an integer literal is the first of the corresponding list in Table 5 in which its value can be represented.
Suffix:
none
, Decimal literal:int
,long int
,long long int
现在再次查看第 5.20 节,我们发现:两者都是常量表达式。
关于c++ - constexpr 函数和硬编码参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33095999/