Skip to content

update(if constexpr): ラムダを使った方法の追加 #577

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 8, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions lang/cpp17/if_constexpr.md
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,26 @@ int main()
}
```

上の例では`false_v`を作ったが、lambda式でも同様のことができる。lambda式はそれが記述された位置から見て最小のスコープ(ブロックスコープ/クラススコープ/名前空間スコープ)で宣言されるクラスとして扱われる。例えば、下の例では`f`というテンプレート関数内で宣言される。テンプレート関数内のクラスは依存名になるため、テンプレートの宣言時に検証されず、テンプレート実体化まで評価を遅らせる事ができる。

```cpp example
#include <type_traits>
template<typename T>
void f(T)
{
if constexpr (std::is_same_v<T, int>)
{
// Tがintのときのみ発動する
static_assert([]{return false;}());
}
}
int main()
{
f(2.4);
f(3);
}
```

`if constexpr`文の条件式内は実体化が起きる。したがって実体化するとコンパイルエラーになるものは書いてはいけない。

```cpp example
Expand Down