I assumed the scenario below: start with an empty vector, click on it some int, then use its size to declare the inline array.
vector<int> vec;
for(decltype(vec.size()) i = 0; i != 10; ++i){
vec.push_back(i);
}
constexpr size_t sz = vec.size();
int arr[sz] = {};
The procedure seems to me intuitive (like a beginner). But this fails with the message below:
testconstexpr2.cpp:22:34: error: call to non-constexpr function βstd::vector<_Tp, _Alloc>::size_type std::vector<_Tp, _Alloc>::size() const [with _Tp = int; _Alloc = std::allocator<int>; std::vector<_Tp, _Alloc>::size_type = long unsigned int]β
testconstexpr2.cpp:23:13: error: size of array βarrβ is not an integral constant-expression
I would rather stick with the built-in array before getting started, like std :: array or dynamic array alloc.
source
share