以下static_assert
应该被丢弃在 的 false 分支中if constexpr
,但由于断言失败而编译失败:
#include <type_traits>
template <class T>
constexpr bool f() {
if constexpr (std::is_same<T, int>::value) return true;
else static_assert(false, "message");
}
int main () {
if constexpr (f<int>()) return 1;
return 0;
}
我希望丢弃的分支if constexpr
不会被评估,因为f
它是用 type 实例化的int
。
使用 Gcc 7.2 (-std=c++17) 编译