I’m currently trying to build code using gcc / g++ like this:
g++-10 -std=c++2a -Wall -Wextra -c -o hw01.o hw01.cpp
(Also the same outcome with gcc-10, gcc, g++)
And I have a static check in my Code:
static_assert(__cplusplus >= 202002L);
which sadly fails every time. As far as I’ve reserched, the used C++ version depends on gcc and it’s version. gcc-10 has version 10.3.0 and the normal gcc command uses version 9.4.0, so both should be able to use C++20 as specified in the build command.
Yet when looking in VsCode, the variable __cplusplus
evaluates to 201402L
, therefore making the assertion fail.
Even when uninstalling / reinstalling the compilers (or sudo apt remove cpp) this problem persists.
Any help? How do I get my system to use a newer C++ version?
PS: I’m working on a Ubuntu WSL (host system is Windows 10)
Edit: Since most recommondations are to neglect the static test and f.e. test for a different value of __cplusplus or simply throw out the test, i’m doing this for a university assignment. The satic test is non-negotiable, can not be changed and also not altered. I have to make the test pass by changing the build value of my local C++ version, I just don’t know how.
2
Answers
Before a future standard version is standardized, the actual value for the future version is not known. Compilers that "support" the new version before standardization will have to pick a value larger than the old one but won’t have the "standard" value.
What you can do, though, is check if
__cplusplus
is greater than the version of the previous standard. For instance,__cplusplus
in C++17 has the value201703
, so the value GCC 10.3 returns of201709
is greater than that, so you "know" that you have a version that is newer than C++17.The
__cplusplus
is meant to provide an answer to the following question: "what version of C++ does this implementation claim to support?" This is a very broad question, particular in terms of the exact definition of "support".GCC 10.3 does not feel that its support of C++20 is sufficient to meet some definition of "support". Therefore, it does not claim to "support" C++20, even if that’s the version you asked for.
This is not something you get to change. There is no compiler option to force GCC to answer the question in a different way.
If you cannot upgrade GCC to a version that is willing to claim "support" for C++20, and you cannot remove or alter the change, then you’re out of luck.