(This is a follow up to this question.)
So I'd like to ask question specifically to understand the standardese quoted in that answer I received, and my exact question is in the title.
Honestly, not even on cppreference I understand what the reason is why the standard says so.
However, here's the minimal example:
#include <array>
int main() {
auto arr = std::array<int,3>{{1,2,3}};
constexpr auto size1 = arr.size(); // OK
auto const& array = arr;
constexpr auto size2 = array.size(); // does not compile
}
which does not compile with the error (the message error is the same with -std=11
/14
/17
/2a
, hence the tags for the two extremes)
$ g++ -std=c++17 deleteme.cpp && ./a.out
deleteme.cpp: In function ‘int main()’:
deleteme.cpp:6:39: error: the value of ‘array’ is not usable in a constant expression
6 | constexpr auto size2 = array.size(); // does not compile
| ^
deleteme.cpp:5:17: note: ‘array’ was not initialized with a constant expression
5 | auto const& array = arr;
| ^~~~~
but it does compile if we remove the &
.
On the other, if I just relied on the note, which reads ‘array’ was not initialized with a constant expression
, I would assume that the following compiles
#include <array>
int main() {
constexpr auto arr = std::array<int,3>{{1,2,3}};
constexpr auto size1 = arr.size(); // OK
constexpr auto& array = arr;
constexpr auto size2 = array.size(); // does not compile
}
but it doesn't and the compiler says (the message error is the same with -std=11
/14
/17
/2a
)
$ g++ -std=c++17 deleteme.cpp && ./a.out
deleteme.cpp: In function ‘int main()’:
deleteme.cpp:5:29: error: ‘arr’ is not a constant expression
5 | constexpr auto& array = arr;
| ^~~
which basically means that arr
is not "a constant expression" even though it is constexpr
, which looks at least a very bad wording to me.