在constexpr

时间:2018-03-07 14:01:29

标签: c++ gcc constexpr throw conditional-compilation

GCC 7.2(-std = c ++ 17)没有编译这个,抱怨由于f而导致throw无效的constexpr:

#include <type_traits>                                                                                                                                                                              

template <class T>                                                                                                                                                                                  
constexpr bool f() noexcept {                                                                                                                                                                       
  if (std::is_same<T, int>  ::value) return true;                                                                                                                                                  
  if (std::is_same<T, float>::value) return false;                                                                                                                                                   

  if (std::is_same<T, int>::value) throw "message";                                                                                                                                               
}                                                                                                                                                                                                   

int main () {                                                                                                                                                                                       
  if constexpr (f<int>()) return 1;                                                                                                                                                               
  return 0;                                                                                                                                                                                    
} 

但控制流程永远不会达到throw

这种行为是由标准规定还是由实施引起的?

附录: Clang 5.0甚至可以编译它,但Gcc 7.2仍然抱怨throw

#include <type_traits>                                                                                                                                                                              

template <class T>                                                                                                                                                                                  
constexpr bool f() noexcept {                                                                                                                                                                       
  if constexpr (std::is_same<T, int>  ::value) return true;                                                                                                                                         
  if constexpr (std::is_same<T, float>::value) return false;                                                                                                                                        

  throw "message";                                                                                                                                                                                  
}                                                                                                                                                                                                   

int main () {                                                                                                                                                                                       
  if constexpr (f<int>()) return 1;                                                                                                                                                                 
  return 0;                                                                                                                                                                                         
} 

0 个答案:

没有答案