constexpr differences between GCC and clang

北城以北 提交于 2020-06-25 10:38:22

问题


The following compiles in GCC 9 but not in clang 10 and I'm wondering which of the two compilers is standard conforming:

template<typename T>
struct A {
  static const T s;
  static const T v;
};

template<typename T>
constexpr const T A<T>::s = T(1);

template<typename T>
constexpr const T A<T>::v = A<T>::s;

int main(int, char**) {
  constexpr auto a = A<double>::v;
  return 0;
}

This is intended to be a minimal example of a bigger issue which is why the fields s and v are explicitly declared as const but are defined as constexpr, this is intentional.

Is GCC correct to compile that code or is clang correct to reject it?


回答1:


Compilers are only required to treat static const variables of integral and enum types as constexpr if they are initialize with a constant expression. This made it possible to use them as array lengths before constexpr was added to the language.



来源:https://stackoverflow.com/questions/61606637/constexpr-differences-between-gcc-and-clang

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!