Skip to content

Commit 3da7c5d

Browse files
committed
fix: INFERIOR_STATIC_ASSERTはやっぱり持ち出さずにサラッと流す (#1260)
1 parent 49d191b commit 3da7c5d

File tree

1 file changed

+2
-26
lines changed

1 file changed

+2
-26
lines changed

lang/cpp17/if_constexpr.md

+2-26
Original file line numberDiff line numberDiff line change
@@ -116,33 +116,9 @@ void g(std::int8_t c) {
116116
117117
### 2段階名前探索における注意点
118118
119-
`constexpr if`文で、実行されない方の`statement`は廃棄文(discarded statement)となり、文の実体化を防ぐ。言い換えると、2段階名前探索における依存名(dependent name)は、廃棄文の場合検証されない。また文が実体化されないのだから通常のif文と同じくもちろん実行時に実行もされない。つまり次の例は意図と異なる挙動を示す。
119+
`constexpr if`文で、実行されない方の`statement`は廃棄文(discarded statement)となり、文の実体化を防ぐ。言い換えると、2段階名前探索における依存名(dependent name)は、廃棄文の場合検証されない。また文が実体化されないのだから通常のif文と同じくもちろん実行時に実行もされない。
120120
121-
```cpp
122-
template<bool> struct inferior_static_assert_failure;
123-
template<> struct inferior_static_assert_failure<true>{ enum { value = 1 }; };
124-
#define INFERIOR_STATIC_ASSERT(B) typedef char inferior_static_assert[sizeof(inferior_static_assert_failure<bool(B)>::value)]
125-
#include <type_traits>
126-
127-
template <typename T>
128-
void f(T)
129-
{
130-
if constexpr (std::is_same_v<T, int>)
131-
{
132-
// Tがintのときのみ評価されてほしい
133-
// 実際は常に評価される
134-
INFERIOR_STATIC_ASSERT(false);
135-
}
136-
}
137-
138-
int main()
139-
{
140-
f(2.4);
141-
f(3);
142-
}
143-
```
144-
145-
CWG 2518が適用されていない環境においては`static_assert`でも同じ現象が発生する。
121+
CWG 2518が適用されていない環境においては`static_assert`を用いる時に直感的ではない挙動を示していた。
146122
147123
```cpp example
148124
#include <type_traits>

0 commit comments

Comments
 (0)