jeudi 20 août 2020

C++ template function returns different value with and without use of constexpr

I have a template function array_size which takes a C-style array by reference and returns its size:

template <typename T, std::size_t N>
constexpr std::size_t array_size(T (&)[N]) noexcept {
  return N;
}

With this, the following works as expected:

int main() {
  int a1[]{1, 2, 3};      // a1's size is 3
  int a2[array_size(a1)]; // a2'2 size is also 3
}

If I remove the constexpr in the template function declaration however, the size of a2 becomes 4295032832. The number also does not change between multiple runs.

My question: What is going on if I omit the constexpr keyword?

I am using gcc 9.3.0 on ubuntu.

Aucun commentaire:

Enregistrer un commentaire