名前空間
変種
操作

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

提供: cppreference.com
< cpp‎ | language

 
 
C + +言語
一般的なトピック
Original:
General topics
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
フロー制御
Original:
Flow control
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
条件付き実行文
Original:
Conditional execution statements
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
繰り返し文
Original:
Iteration statements
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
文をジャンプします
Original:
Jump statements
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
機能します
Original:
Functions
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
関数の宣言
ラムダ関数の宣言
関数テンプレート
の歴史。インライン指定
例外仕様 (廃止予定)
noexcept指定子 (C++11)
例外
Original:
Exceptions
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
名前空間
Original:
Namespaces
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
タイプ
Original:
Types
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
decltype specifier (C++11)
指定子
Original:
Specifiers
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
CV指定
貯蔵期間指定
constexprの指定子 (C++11)
自動指定 (C++11)
alignas指定子 (C++11)
初期化
Original:
Initialization
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
リテラル
Original:
Literals
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Original:
Expressions
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
代替表現
ユーティリティ
Original:
Utilities
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
タイプ
Original:
Types
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
typedef declaration
型の別名宣言 (C++11)
属性 (C++11)
キャストします
Original:
Casts
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
暗黙の型変換
const_cast conversion
static_cast conversion
dynamic_cast conversion
reinterpret_cast conversion
Cスタイルキャストと機能
メモリの割り当て
Original:
Memory allocation
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
クラス
Original:
Classes
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
クラス固有の機能特性
Original:
Class-specific function properties
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
明示 (C++11)
静的
特殊なメンバ関数
Original:
Special member functions
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
テンプレート
Original:
Templates
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
クラステンプレート
関数テンプレート
テンプレートの特殊化
パラメーターパック (C++11)
その他
Original:
Miscellaneous
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 - 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 iteration
constexpr int factorial(int n)
{
    return n <= 1 ? 1 : (n * factorial(n-1));
}
 
// literal class
class conststr {
    const char * p;
    std::size_t sz;
 public:
    template<std::size_t N>
    constexpr conststr(const char(&a)[N]) : p(a), sz(N-1) {}
    // constexpr functions signal errors by throwing exceptions from operator ?:
    constexpr char operator[](std::size_t n) {
        return n < sz ? p[n] : throw std::out_of_range("");
    }
    constexpr std::size_t size() { return sz; }
};
constexpr std::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 testing
template<int n> struct constN {
    constN() { std::cout << n << '\n'; }
};
 
int main()
{
    std::cout << "4! = " ;
    constN<factorial(4)> out1; // computed at compile time
 
    volatile int 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
}

Output:

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