Skip to content

Commit

Permalink
constexprコンストラクタがbodyを持てるようになった件を追記
Browse files Browse the repository at this point in the history
N3652 Change in [dcl.constexpr] (7.1.5)/4
  • Loading branch information
wx257osn2 committed Jan 4, 2023
1 parent 79d9ed9 commit bbf5105
Showing 1 changed file with 14 additions and 0 deletions.
14 changes: 14 additions & 0 deletions lang/cpp14/relaxing_constraints_on_constexpr.md
Expand Up @@ -12,6 +12,7 @@ C++11で、汎用定数式の機能である[`constexpr`](/lang/cpp11/constexpr.
- 変数の書き換えを許可
- 戻り値型(リテラル型)として、`void`を許可 / 戻り値型や関数引数で非`const`参照を許可
- `constexpr`非静的メンバ関数の、暗黙の`const`修飾を削除
- `constexpr`コンストラクタがbodyを持てるようになった


## 仕様
Expand Down Expand Up @@ -175,6 +176,19 @@ C++14ではこの仕様が削除され、`const`か非`const`かを、明示的
※この変更によって、既存コードの互換性は壊れない。


### `constexpr`コンストラクタがbodyを持てるようになった
C++11では、`constexpr`コンストラクタのbodyには以下の要素しか持たせることを許されていなかった:

- ヌル文
- `static_assert`
- クラスや列挙型を定義しない、別の型名定義
- `using`宣言と、`using`ディレクティブ

これは事実上`constexpr`コンストラクタのbodyが空でなければいけないことを意味している。

C++14では`constexpr`コンストラクタのbodyに関する制約は一般の`constexpr`関数に従うようになったため、body内でローカル変数を定義したり引数に応じたメンバ変数の書き換えを行ったりすることが許可された。


## この機能が必要になった背景・経緯
C++は直交性を重視して設計されており、直接関係ない機能同士を組み合わせて使用できる。しかし、C++11での`constexpr`は、その制限によって、ほかの機能(インスタンス、`for`ループ、変数書き換え、例外等)とうまく組み合わせられなかった。

Expand Down

0 comments on commit bbf5105

Please sign in to comment.