constexpr specifier (C++11およびそれ以降)
提供: cppreference.com
![]() | このページは、Google 翻訳を使って英語版から機械翻訳されました。 翻訳には誤りや奇妙な言い回しがあるかもしれません。文章の上にポインタをおくと、元の文章が見れます。誤りを修正して翻訳を改善する手助けをしてください。翻訳についての説明は、ここをクリックしてください。 |
constexpr
- 変数や関数の値はコンパイル時に計算できることを指定しますOriginal:constexpr
- specifies that the value of a variable or function can be computed at compile timeThe 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.
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.
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 ofconstexpr
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.
You can help to correct and verify the translation. Click here for instructions.
- それは仮想であってはなりませんOriginal:it must not be virtualThe 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 beLiteralType
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 typeThe 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 statementsThe 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 declarationsThe text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions. - 宣言を使用Original:using declarationsThe text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions. - ディレクティブを使用していますOriginal:using directivesThe 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 onereturn
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.
You can help to correct and verify the translation. Click here for instructions.
- その各パラメータはリテラル型でなければなりませんOriginal:each of its parameters must be literal typeThe 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 classesThe 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 statementsThe 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 declarationsThe text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions. - 宣言を使用Original:using declarationsThe text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions. - ディレクティブを使用していますOriginal:using directivesThe 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 blockThe 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 theinitializer_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 expressionThe 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 expressionThe text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
[編集]キーワード
[編集]例
階乗と文字列リテラルを拡張リテラル型を計算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.
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