dav*_*ide 8 c++ instantiation static-assert constexpr if-constexpr
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;
}
Run Code Online (Sandbox Code Playgroud)
我希望丢弃的分支if constexpr
不会被评估,因为它f
是用 type 实例化的int
。
用 Gcc 7.2 (-std=c++17) 编译