Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      consteval specifier(since C++20)

      From cppreference.com
      <cpp‎ |language
       
       
      C++ language
      General topics
      Flow control
      Conditional execution statements
      Iteration statements (loops)
      Jump statements
      Functions
      Function declaration
      Lambda function expression
      inline specifier
      Dynamic exception specifications(until C++17*)
      noexcept specifier(C++11)
      Exceptions
      Namespaces
      Types
      Specifiers
      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
      Special member functions
      Templates
      Miscellaneous
       
      Declarations
       
      • consteval - specifies that a function is animmediate function, that is, every call to the function must produce a compile-time constant

      Contents

      [edit]Explanation

      Theconsteval specifier declares a function or function template to be animmediate function, that is, everypotentially-evaluated call to the function must (directly or indirectly) produce a compile timeconstant expression.

      An immediate function is aconstexpr function, subject to its requirements as the case may be. Same asconstexpr, aconsteval specifier impliesinline. However, it may not be applied to destructors, allocation functions, or deallocation functions.

      A function or function template declaration specifyingconsteval may not also specifyconstexpr, and any redeclarations of that function or function template must also specifyconsteval.

      Apotentially-evaluated invocation of an immediate function whose innermost non-block scope is not afunction parameter scope of an immediate function or the true-branch of aconsteval if statement(since C++23) must produce a constant expression; such an invocation is known as animmediate invocation.

      constevalint sqr(int n){return n*n;}constexprint r= sqr(100);// OK int x=100;int r2= sqr(x);// Error: Call does not produce a constant constevalint sqrsqr(int n){return sqr(sqr(n));// Not a constant expression at this point, but OK} constexprint dblsqr(int n){return2* sqr(n);// Error: Enclosing function is not consteval// and sqr(n) is not a constant}

      Anidentifier expression that denotes an immediate function may only appear within a subexpression of an immediate invocation or within animmediate function context (i.e. a context mentioned above, in which a call to an immediate function needs not to be a constant expression). A pointer or reference to an immediate function can be taken but cannot escape constant expression evaluation:

      constevalint f(){return42;}constevalauto g(){return&f;}constevalint h(int(*p)()= g()){return p();}constexprint r= h();// OKconstexprauto e= g();// ill-formed: a pointer to an immediate function is// not a permitted result of a constant expression

      [edit]Notes

      Feature-test macroValueStdFeature
      __cpp_consteval201811L(C++20)Immediate functions
      202211L(C++23)
      (DR20)
      Makingconsteval propagate up

      [edit]Keywords

      consteval

      [edit]Example

      Run this code
      #include <iostream> // This function might be evaluated at compile-time, if the input// is known at compile-time. Otherwise, it is executed at run-time.constexprunsigned factorial(unsigned n){return n<2?1: n* factorial(n-1);} // With consteval we enforce that the function will be evaluated at compile-time.constevalunsigned combination(unsigned m,unsigned n){return factorial(n)/ factorial(m)/ factorial(n- m);} static_assert(factorial(6)==720);static_assert(combination(4,8)==70); int main(int argc,constchar*[]){constexprunsigned x{factorial(4)};std::cout<< x<<'\n'; [[maybe_unused]]unsigned y= factorial(argc);// OK//  unsigned z = combination(argc, 7); // error: 'argc' is not a constant expression}

      Output:

      24

      [edit]See also

      constexpr specifier(C++11) specifies that the value of a variable or function can be computed at compile time[edit]
      constinit specifier(C++20) asserts that a variable has static initialization, i.e.zero initialization andconstant initialization[edit]
      constant expression defines anexpression that can be evaluated at compile time
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/language/consteval&oldid=179507"

      [8]ページ先頭

      ©2009-2025 Movatter.jp