The Wayback Machine - https://web.archive.org/web/20180228105858/http://ja.cppreference.com:80/w/cpp/language/constexpr
名前空間
変種
操作

constexpr specifier (C++11およびそれ以降)

提供: cppreference.com
< cpp‎ | language

 
 
C++言語
一般的なトピック
フロー制御
条件付き実行文
繰り返し文 (ループ)
ジャンプ文
関数
関数宣言
ラムダ関数宣言
inline 指定子
例外仕様(廃止予定)
noexcept 指定子(C++11)
例外
名前空間
指定子
decltype(C++11)
auto(C++11)
alignas(C++11)
const/volatile
constexpr(C++11)
記憶域期間指定子
初期化
代替表現
リテラル
ブーリアン - 整数 - 浮動小数点
文字 - 文字列 - nullptr(C++11)
ユーザ定義(C++11)
ユーティリティ
アトリビュート(C++11)
typedef 宣言
型エイリアス宣言(C++11)
キャスト
暗黙の変換 - 明示的な変換
static_cast - dynamic_cast
const_cast - reinterpret_cast
メモリ確保
クラス
クラス固有の関数特性
特別なメンバ関数
テンプレート
その他
 
  • constexpr - 変数や関数の値はコンパイル時に計算できることを指定します
    Original:
    constexpr - specifies that the value of a variable or function can be computed at compile time
    The text has been machine-translated via Google Translate.
    You can help to correct and verify the translation. Click here for instructions.

[編集]説明

constexprのみが許可され、コンパイル時の場所定数式指定子は、それがコンパイル時に関数や変数の値を評価することが可能であることを宣言し、それらはその後使用することができます。 constexprconst暗示.
Original:
constexpr specifier declares that it is possible to evaluate the value of the function or variable at compile time, they then can be used where only compile time 定数式 are allowed. constexpr implies const.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
'constexpr変数'は、次の要件を満たす必要があります
Original:
constexpr variables must satisfy the following requirements:
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
  • それはすぐに構築するか、値を割り当てなければなりません.
    Original:
    it must be immediately constructed or assigned a value.
    The text has been machine-translated via Google Translate.
    You can help to correct and verify the translation. Click here for instructions.
  • コンストラクターのパラメーターまたはリテラル値のみ、constexpr変数と関数が含まれている必要が割り当てられる値.
    Original:
    the constructor parameters or the value to be assigned must contain only literal values, constexpr variables and functions.
    The text has been machine-translated via Google Translate.
    You can help to correct and verify the translation. Click here for instructions.
  • オブジェクト(暗黙的または明示的な)を構築するために使用されるコンストラクタはconstexprコンストラクタの要件を満たす必要があります。明示的なコンストラクタの場合には、それはconstexpr指定しておく必要があります.
    Original:
    the constructor used to construct the object (either implicit or explicit) must satisfy the requirements of constexpr constructor. In the case of explicit constructor, it must have constexpr specified.
    The text has been machine-translated via Google Translate.
    You can help to correct and verify the translation. Click here for instructions.
'constexpr機能'は、次の要件を満たす必要があります
Original:
constexpr functions must satisfy the following requirements:
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
  • それは仮想であってはなりません
    Original:
    it must not be virtual
    The text has been machine-translated via Google Translate.
    You can help to correct and verify the translation. Click here for instructions.
  • その戻り値の型はLiteralTypeでなければなりません
    Original:
    its return type must be LiteralType
    The text has been machine-translated via Google Translate.
    You can help to correct and verify the translation. Click here for instructions.
  • その各パラメータはリテラル型でなければなりません
    Original:
    each of its parameters must be literal type
    The text has been machine-translated via Google Translate.
    You can help to correct and verify the translation. Click here for instructions.
  • 関数の本体には、削除されるか、またはデフォルト値または以下を含むだけである必要があります
    Original:
    the function body must be either deleted or defaulted or contain only the following:
    The text has been machine-translated via Google Translate.
    You can help to correct and verify the translation. Click here for instructions.
  • 空文
    Original:
    null statements
    The text has been machine-translated via Google Translate.
    You can help to correct and verify the translation. Click here for instructions.
  • static_assert宣言
    Original:
    static_assert declarations
    The text has been machine-translated via Google Translate.
    You can help to correct and verify the translation. Click here for instructions.
  • typedefクラスや列挙型を定義していない宣言とエイリアス宣言
    Original:
    typedef declarations and エイリアス declarations that do not define classes or enumerations
    The text has been machine-translated via Google Translate.
    You can help to correct and verify the translation. Click here for instructions.
  • 宣言を使用
    Original:
    using declarations
    The text has been machine-translated via Google Translate.
    You can help to correct and verify the translation. Click here for instructions.
  • ディレクティブを使用しています
    Original:
    using directives
    The text has been machine-translated via Google Translate.
    You can help to correct and verify the translation. Click here for instructions.
  • リテラル値のみ、return変数と関数が含まれています正確に1つのconstexpr文.
    Original:
    exactly one return statement that contains only literal values, constexpr variables and functions.
    The text has been machine-translated via Google Translate.
    You can help to correct and verify the translation. Click here for instructions.
'constexprコンストラクタ'次の要件を満たす必要があります
Original:
constexpr constructor must satisfy the following requirements:
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
  • その各パラメータはリテラル型でなければなりません
    Original:
    each of its parameters must be literal type
    The text has been machine-translated via Google Translate.
    You can help to correct and verify the translation. Click here for instructions.
  • クラスには仮想基底クラスを持っていてはいけない
    Original:
    the class must have no virtual base classes
    The text has been machine-translated via Google Translate.
    You can help to correct and verify the translation. Click here for instructions.
  • コンストラクタ本体が削除されるか、またはデフォルト値または以下を含むだけである必要があります
    Original:
    the constructor body must be either deleted or defaulted or contain only the following:
    The text has been machine-translated via Google Translate.
    You can help to correct and verify the translation. Click here for instructions.
  • 空文
    Original:
    null statements
    The text has been machine-translated via Google Translate.
    You can help to correct and verify the translation. Click here for instructions.
  • static_assert宣言
    Original:
    static_assert declarations
    The text has been machine-translated via Google Translate.
    You can help to correct and verify the translation. Click here for instructions.
  • typedefクラスや列挙型を定義していない宣言とエイリアス宣言
    Original:
    typedef declarations and エイリアス declarations that do not define classes or enumerations
    The text has been machine-translated via Google Translate.
    You can help to correct and verify the translation. Click here for instructions.
  • 宣言を使用
    Original:
    using declarations
    The text has been machine-translated via Google Translate.
    You can help to correct and verify the translation. Click here for instructions.
  • ディレクティブを使用しています
    Original:
    using directives
    The text has been machine-translated via Google Translate.
    You can help to correct and verify the translation. Click here for instructions.
  • コンストラクタは関数tryブロックを持ってはいけません
    Original:
    the constructor must not have a function-try block
    The text has been machine-translated via Google Translate.
    You can help to correct and verify the translation. Click here for instructions.
  • すべての基底クラスとすべての非静的メンバはinitializer_listやブレースか等しい初期化子のいずれかによって、初期化する必要があります。関係するすべてのコンストラクタはconstexprのコンストラクタでなければならず、すべてのブレースか等しい初期化子のすべての句が定数式でなければなりません。終了
    Original:
    every base class and every non-static member must be initialized, either in the initializer_list or by brace-or-equal initializer. End every constructor involved must be a constexpr constructor and every clause of every brace-or-equal initializer must be a constant expression
    The text has been machine-translated via Google Translate.
    You can help to correct and verify the translation. Click here for instructions.
  • 関係するすべての暗黙的な変換は定数式でなければなりません
    Original:
    every implicit conversion involved must be a constant expression
    The text has been machine-translated via Google Translate.
    You can help to correct and verify the translation. Click here for instructions.

[編集]キーワード

constexpr

[編集]

階乗と文字列リテラルを拡張リテラル型を計算constexprの関数の定義

Original:

Definition of a constexpr function which computes factorials and a literal type that extends string literals

The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

#include <iostream>#include <stdexcept>   // constexpr functions use recursion rather than iterationconstexprint factorial(int n){return n <=1?1:(n * factorial(n-1));}   // literal classclass conststr {constchar* p;std::size_t sz;public:template<std::size_t N>constexpr conststr(constchar(&a)[N]): p(a), sz(N-1){}// constexpr functions signal errors by throwing exceptions from operator ?:constexprchar operator[](std::size_t n){return n < sz ? p[n]:throwstd::out_of_range("");}constexprstd::size_t size(){return sz;}};constexprstd::size_t countlower(conststr s, std::size_t n =0, std::size_t c =0){return n == s.size()? c : s[n]>='a'&& s[n]<='z'? countlower(s, n+1, c+1): countlower(s, n+1, c);}   // output function that requires a compile-time constant, for testingtemplate<int n>struct constN { constN(){std::cout<< n <<'\n';}};   int main(){std::cout<<"4! = "; constN<factorial(4)> out1;// computed at compile time   volatileint k =8;std::cout<< k <<"! = "<< factorial(k)<<'\n';// computed at run time   std::cout<<"Number of lowercase letters in \"Hello, world!\" is "; constN<countlower("Hello, world!")> out2;// implicitly converted to conststr}

出力:

4! = 24 8! = 40320 Number of lowercase letters in "Hello, world!" is 9
close