如果 constexpr 没有被丢弃,Static_assert 在里面

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) 编译