Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      std::is_constant_evaluated

      From cppreference.com
      <cpp‎ |types
       
       
      Utilities library
       
      Defined in header<type_traits>
      constexprbool is_constant_evaluated()noexcept;
      (since C++20)

      Detects whether the function call occurs within a constant-evaluated context. Returnstrue if the evaluation of the call occurs within the evaluation of an expression or conversion that ismanifestly constant-evaluated; otherwise returnsfalse.

      To determine whether initializers of following variables are manifestly constant-evaluated, compilers may first perform a trial constant evaluation:

      • variables with reference type or const-qualified integral or enumeration type;
      • static and thread local variables.

      It is not recommended to depend on the result in this case.

      int y=0;constint a= std::is_constant_evaluated()? y:1;// Trial constant evaluation fails. The constant evaluation is discarded.// Variable a is dynamically initialized with 1 constint b= std::is_constant_evaluated()?2: y;// Constant evaluation with std::is_constant_evaluated() == true succeeds.// Variable b is statically initialized with 2

      Contents

      [edit]Parameters

      (none)

      [edit]Return value

      true if the evaluation of the call occurs within the evaluation of an expression or conversion that is manifestly constant-evaluated; otherwisefalse.

      [edit]Possible implementation

      // This implementation requires C++23 if consteval.constexprbool is_constant_evaluated()noexcept{if consteval{returntrue;}else{returnfalse;}}

      [edit]Notes

      When directly used as the condition ofstatic_assert declaration orconstexpr if statement,std::is_constant_evaluated() always returnstrue.

      Becauseif consteval is absent in C++20,std::is_constant_evaluated is typically implemented using a compiler extension.

      Feature-test macroValueStdFeature
      __cpp_lib_is_constant_evaluated201811L(C++20)std::is_constant_evaluated

      [edit]Example

      Run this code
      #include <cmath>#include <iostream>#include <type_traits> constexprdouble power(double b,int x){if(std::is_constant_evaluated()&&!(b==0.0&& x<0)){// A constant-evaluation context: Use a constexpr-friendly algorithm.if(x==0)return1.0;double r{1.0};double p{x>0? b:1.0/ b};for(auto u=unsigned(x>0? x:-x); u!=0; u/=2){if(u&1)                r*= p;            p*= p;}return r;}else{// Let the code generator figure it out.returnstd::pow(b,double(x));}} int main(){// A constant-expression contextconstexprdouble kilo= power(10.0,3);int n=3;// Not a constant expression, because n cannot be converted to an rvalue// in a constant-expression context// Equivalent to std::pow(10.0, double(n))double mucho= power(10.0, n); std::cout<< kilo<<" "<< mucho<<"\n";// (3)}

      Output:

      1000 1000

      [edit]See also

      constexpr specifier(C++11) specifies that the value of a variable or function can be computed at compile time[edit]
      consteval specifier(C++20) specifies that a function is animmediate function, that is, every call to the function must be in a constant evaluation[edit]
      constinit specifier(C++20) asserts that a variable has static initialization, i.e.zero initialization andconstant initialization[edit]
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/types/is_constant_evaluated&oldid=179319"

      [8]ページ先頭

      ©2009-2025 Movatter.jp