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?
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.
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.
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';
The behavior of a program that adds specializations for extent or extent_v (since C++17) is undefined.
Sorry (not sorry) for the RTFM, but from std::extent
at cppreference.com:
If
T
is an array type, provides the member constantvalue
equal to the number of elements along theN
th dimension of the array, ifN
is in[0, std::rank<T>::value)
. For any other type, or ifT
is array of unknown bound along its first dimension andN
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
Ain't C++ great?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With