1

Similar question has been already asked.

However, I'd like to understand why, for arrays, first line below does not compile while second line compiles (g++, clang++, icpx):

template<int N> constexpr float x[2]; // ERROR - var not initialized.
template<int N> constexpr float y[N];

// Can now specialize, e.g.:
template<> constexpr float y<2>[2] = {0.0f, 1.0f};
user2052436
  • 4,321
  • 1
  • 25
  • 46
  • 1
    This seems like more of a compiler implementation detail, probably stemming from the fact that the second version uses the template parameter in the declaration body. The moment that you try to instantiate `y` you will get an error. – ChrisB Apr 15 '23 at 03:18
  • @ChrisB https://godbolt.org/z/rbGEPWc9P – user2052436 Apr 15 '23 at 03:25
  • 1
    The reasoning of the linked question still applies in more recent standards (I checked N4917) so this should indeed be reported as an error, and the compilers behave incorrectly here. Interestingly, MSVC used to do so but changed it's mind in 19.27 (https://godbolt.org/z/1bn8rK7Eh). – ChrisB Apr 15 '23 at 03:56

0 Answers0