Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

constexpr function must have one argument value?

According to this, a function declared with constexpr must satisfy a few requirements, one of which is as follows:

there exists at least one argument value such that an invocation of the function could be an evaluated subexpression of a core constant expression ...

Well, constexpr function can have no arguments:

constexpr int Bar( /* empty */ ) { return 0xFF; }
constexpr int value = Bar(); // Valid expression

constexpr function that is invoked as a sub-routine can not determine the whole expression to be core constant expression either.

So what does it mean by one argument value must exist?

[Update for future readers]

Apparently the description about the requirements of constexpr function has been fixed since this question from:

there exists at least one argument value such that an invocation of the function could be an evaluated subexpression of a core constant expression ...

to:

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 ...

like image 246
Dean Seo Avatar asked Sep 27 '16 11:09

Dean Seo


1 Answers

The quote from en.cppreference.com is not accurate in regards to the standard, the real quote is (§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.

Which basically says that there must exist one valid set of arguments (the empty set being a valid one in your case).

like image 73
Holt Avatar answered Sep 22 '22 05:09

Holt