Namespaces
Variants
Views
Actions

consteval specifier (since C++20)

From cppreference.com
< cpp‎ | language
Revision as of 01:35, 13 May 2020 by Amirk (Talk | contribs)

 
 
C++ language
General topics
Flow control
Conditional execution statements
if
Iteration statements (loops)
for
range-for (C++11)
Jump statements
Functions
Function declaration
Lambda function expression
inline specifier
Dynamic exception specifications (until C++17*)
noexcept specifier (C++11)
Exceptions
Namespaces
Types
Specifiers
const/volatile
decltype (C++11)
auto (C++11)
constexpr (C++11)
consteval (C++20)
constinit (C++20)
Storage duration specifiers
Initialization
Expressions
Alternative representations
Literals
Boolean - Integer - Floating-point
Character - String - nullptr (C++11)
User-defined (C++11)
Utilities
Attributes (C++11)
Types
typedef declaration
Type alias declaration (C++11)
Casts
Memory allocation
Classes
Class-specific function properties
explicit (C++11)
static

Special member functions
Templates
Miscellaneous
 
 
  • consteval - specifies that a function is an immediate function, that is, every call to the function must produce a compile-time constant

Contents

Explanation

The consteval specifier declares a function or function template to be an immediate function, that is, every potentially evaluated call (i.e. call out of an unevaluated context) to the function must (directly or indirectly) produce a compile time constant expression. It may not be applied to destructors, allocation functions, or deallocation functions. A consteval specifier implies inline. At most one of the constexpr, consteval, and constinit specifiers is allowed to appear within the same sequence of declaration specifiers. If any declaration of a function or function template contains a consteval specifier, then all declarations of that function or function template must contain that specifier.

An immediate function is a constexpr function, and must satisfy the requirements applicable to constexpr functions or constexpr constructors, as the case may be.

A potentially evaluated invocation of an immediate function whose innermost non-block scope is not a function parameter scope of an immediate function must produce a constant expression; such an invocation is known as an immediate invocation.

consteval int sqr(int n) {
  return n*n;
}
constexpr int r = sqr(100);  // OK, the sent argument is constexpr
 
int x = 100;
int r2 = sqr(x);  // Error: sending an argument that is not known at compile time
 
consteval int sqrsqr(int n) {
  return sqr(sqr(n)); // OK, the sent argument is known at compile time in both calls
}
 
constexpr int dblsqr(int n) {
  return 2*sqr(n); // Error: Enclosing function is not consteval, thus n is not a constexpr
}
 
// however, calling a constexpr function from a consteval one:
 
constexpr int another_sqr(int n) {
  return n * n; // OK, result can be constexpr or not
}
 
consteval int another_sqrsqr(int n) {
  return another_sqr(another_sqr(n)); // OK, the sent argument is known at compile time
}
 
// note that consteval arguments by themselves are not usable as constant expressions:
 
consteval int get(std::pair<int, int> p, int index) {
  // index is known at compile time but is not a constexpr by itself
  return std::get<index>(p); // Error: index is not constexpr
}

An identifier expression that denotes an immediate function may only appear within a subexpression of an immediate invocation or within an immediate function context. A pointer or reference to an immediate function can be taken but cannot escape constant expression evaluation:

consteval int f() { return 42; }
consteval auto g() { return &f; }
consteval int h(int (*p)() = g()) { return p(); }
constexpr int r = h();   // OK
constexpr auto e = g();  // ill-formed: a pointer to an immediate function is
                         // not a permitted result of a constant expression

Keywords

consteval

Example

See also