jeudi 31 mars 2016

Why does std::extent applied to auto& yield zero?

I was experimenting with constexpr auto and string literals to get character arrays I could use with std::begin in a generic way, when I ran into something I couldn't explain: the expression std::extent<decltype(foo)>::value, where foo is declared using auto reference, yields zero.

 #include <iostream>
 #include <type_traits>

 namespace {
   auto& ARRAY_REFERENCE = "foo";

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

 int main() {
   std::cerr <<
     "std::extent applied to ARRAY_REFERENCE: " << std::extent<decltype(ARRAY_REFERENCE)>::value << "\n"
     "Number of elements in ARRAY_REFERENCE: " << numberOfElementsIn(ARRAY_REFERENCE) << "\n"
     ;
   return 0;
 }

The code above gives me the output

std::extent applied to ARRAY_REFERENCE: 0
Number of elements in ARRAY_REFERENCE: 4

Why doesn't the expression involving std::extent evaluate to 4?

Aucun commentaire:

Enregistrer un commentaire