Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

c++20 seems to be not supporting constexpr vector – is my installation incorrect

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 :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading