How to use C++ 20 in g++

I am trying to access std::popcount, but it seems like it's only there in C++ 20.

When I try compiling with g++ -std=c++20 main.cpp, it says g++: error: unrecognized command line option '-std=c++20'; did you mean '-std=c++03'

How do I tell g++ to use c++ 20?

I am using Ubuntu 18.04


C++20 features are available since GCC 8.

To enable C++20 support, add the command-line parameter

-std=c++20

For G++ 9 and later use

-std=c++2a

Or, to enable GNU extensions in addition to C++20 features, add

-std=gnu++20


I would try updating gcc. C++ 20 was introduced in gcc version 8 which is pretty new.


If it's an option you can update to Ubuntu 20.04 LTS which includes GCC version 9 out of the box. This would enable you to use C++ 20 and thus std::popcount

Note: use -std=c++2a in GCC 9 and earlier