constexpr 函数和硬编码参数

constexpr function and hardcoded arguments

所以通常 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 在编译期间都是已知的。

所以我的问题是现实生活中怎么样?这个函数会在 运行 时间或编译时间

中执行

根据标准草案N4527 7.1.5/3 constexpr说明符[dcl.constexpr]强调我的):

The definition of a constexpr function shall satisfy the following constraints:

(3.1) — it shall not be virtual (10.3);

(3.2) — its return type shall be a literal type;

(3.3) — each of its parameter types shall be a literal type;

...

因此,调用 function(5,6); 满足 constexpr 函数的定义,并且将在编译时执行。

此外,您可以使用std::integral_constant:

自行测试
#include <iostream>
#include <type_traits>

constexpr int fun(int x, int y) {
   return x + y;
}

int main() {
    std::cout << std::integral_constant<int, fun(5, 6)>::value << std::endl;
}

LIVE DEMO

如果fun中的输入参数不是constexpr编译将失败。

constexpr int fun(int x, int y) { return x+y; }
fun(5,6) // << constant expression?

tl;dr

56 是常量表达式。因此 fun(5,6) 也是一个常量表达式,将在编译时进行评估,这是强制性的(例如非类型模板)。

东西... 我快速浏览了该标准,希望我没有遗漏任何要点。

我们已经从@42的回答中得知:

  • 根据 N4527 intconstexpr 函数的有效参数类型 因为它是文字类型(因为它是标量类型根据同一文档的 §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 function with constant expression arguments",因此它们是核心常量表达式(除非它们在使用时未定义)。

因此,如果 56 是常量表达式,则 fun(5,6) 是常量表达式,如果 fun 是有效的 constexpr 函数 并在使用前定义。给定函数满足 §7.1.5/3 中要求的约束并且是有效的 constexpr 函数.

根据 §2.13.2

56 都是 类型 int 的整数文字

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 我们看到:两者都是常量表达式。