constexpr 函数必须有一个参数值?
constexpr function must have one argument value?
根据this,用constexpr
声明的函数必须满足几个要求,其中之一如下:
there exists at least one argument value such that an invocation of the function could be an evaluated subexpression of a core constant expression ...
嗯,constexpr
函数可以 没有参数:
constexpr int Bar( /* empty */ ) { return 0xFF; }
constexpr int value = Bar(); // Valid expression
constexpr
作为子程序调用的函数也不能确定整个表达式为核心常量表达式
那么一个参数值必须存在是什么意思?
[为未来的读者更新]
显然关于 constexpr function
要求的描述已经修复,因为这个问题来自:
there exists at least one argument value such that an invocation of the function could be an evaluated subexpression of a core constant expression ...
至:
there exists at least one set of argument values such that an invocation of the function could be an evaluated subexpression of a core constant expression ...
"One argument value"这里的意思是"one set of arguments"。空函数有一个可能的参数集,即空集。
在你的例子中,这个空参数集确实导致了一个有效的 constexpr
调用,所以一切都很好。
en.cppreference.com 的引述在标准方面不准确,真正的引述是 (§7.1.5/5):
For a constexpr
function or constexpr
constructor that is neither defaulted nor a template, if no argument values exist such that an invocation of the function or constructor could be an evaluated subexpression of a core constant expression (5.20) [...] the program is ill-formed; no diagnostic required.
这基本上是说必须存在一组有效的参数(在您的情况下,空集是有效的)。
根据this,用constexpr
声明的函数必须满足几个要求,其中之一如下:
there exists at least one argument value such that an invocation of the function could be an evaluated subexpression of a core constant expression ...
嗯,constexpr
函数可以 没有参数:
constexpr int Bar( /* empty */ ) { return 0xFF; }
constexpr int value = Bar(); // Valid expression
constexpr
作为子程序调用的函数也不能确定整个表达式为核心常量表达式
那么一个参数值必须存在是什么意思?
[为未来的读者更新]
显然关于 constexpr function
要求的描述已经修复,因为这个问题来自:
there exists at least one argument value such that an invocation of the function could be an evaluated subexpression of a core constant expression ...
至:
there exists at least one set of argument values such that an invocation of the function could be an evaluated subexpression of a core constant expression ...
"One argument value"这里的意思是"one set of arguments"。空函数有一个可能的参数集,即空集。
在你的例子中,这个空参数集确实导致了一个有效的 constexpr
调用,所以一切都很好。
en.cppreference.com 的引述在标准方面不准确,真正的引述是 (§7.1.5/5):
For a
constexpr
function orconstexpr
constructor that is neither defaulted nor a template, if no argument values exist such that an invocation of the function or constructor could be an evaluated subexpression of a core constant expression (5.20) [...] the program is ill-formed; no diagnostic required.
这基本上是说必须存在一组有效的参数(在您的情况下,空集是有效的)。