I just finished upgrading my compiler to C++20 on ubuntu 20.04. g++ version gives me the following output :
c++ (Ubuntu 10.3.0-1ubuntu1~20.04) 10.3.0
I am trying the following code as suggested on stackoverflow
constexpr int f() {
std::vector<int> v = {1, 2, 3};
return v.size();
}
int main() {
static_assert(f() == 3);
}
But I am getting the following error :
error: variable ‘v’ of non-literal type ‘std::vector<int>’ in ‘constexpr’ function
Am I going wrong somewhere. Or is my installation incorrect
>Solution :
You need to upgrade gcc to at least 12 to get the C++23 constexpr support for non-literal types as std::vector<int>.
From compiler support @ cppreference:
| gcc | clang | EDG eccp | ||
|---|---|---|---|---|
Non-literal variables (and labels and gotos) in constexpr functions |
P2242R3 | 12 | 15 | 6.3 |
Feature test:
__cpp_constexpr >= 202110L