8

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

4

0 回答 0