Kranar
Kranar

Reputation: 1156

constexpr differences between GCC and clang

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?

Upvotes: 5

Views: 363

Answers (1)

Isaac Clancy
Isaac Clancy

Reputation: 418

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.

Upvotes: 1

Related Questions