Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c++

c++11

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?

like image 610
psyill Avatar asked Mar 31 '16 12:03

psyill


People also ask

How to check if a datafile is autoextensible?

You can check the autoextensible of datafiles using the following script. select file_name,tablespace_name,maxbytes from dba_data_files where autoextensible='NO'; If the autoextensible of datafile is NO, autoextensible feature is not enabled for the related datafile, and datafile will not auto extend.

Why can't I write to an auto variable in C++?

A confused question, but it shows how far expectations on C++ have come (and how beginner friendly it can be). The reason you can't "write" to your auto variable is that it's a const char * or const char [1], because that is the type of any string constant.

What happens if tablespaces’ autoextend is not enabled?

If tablespaces’ autoextend is not enabled, then you are alerted when the tablespace reaches its critical or warning threshold size. How to check autoextend on tablespace in oracle You can check the autoextensible of datafiles using the following script. select file_name,tablespace_name,maxbytes from dba_data_files where autoextensible='NO';

What is the behavior of a program with extent_V in C++?

The behavior of a program that adds specializations for extent or extent_v (since C++17) is undefined.


1 Answers

Sorry (not sorry) for the RTFM, but from std::extent at cppreference.com:

If T is an array type, provides the member constant value equal to the number of elements along the Nth dimension of the array, if N is in [0, std::rank<T>::value). For any other type, or if T is array of unknown bound along its first dimension and N is 0, value is 0.

Your T is not an array type; it is a reference type.

You can fix this with std::remove_reference:

std::extent<std::remove_reference<decltype(ARRAY_REFERENCE)>::type>::value

(live demo)

Ain't C++ great?

like image 170
Lightness Races in Orbit Avatar answered Sep 18 '22 15:09

Lightness Races in Orbit