HARSHIT BAJPAI
HARSHIT BAJPAI

Reputation: 670

I am unable to print out the value 201703L for _cplusplus macro for g++

I am trying to get my compiler to use c++ version 17. Here is the code snippet I am using to check if I can compile using c++17 with my g++ compiler.

#include<iostream>

int main() {
    if (__cplusplus == 201703L) std::cout << "C++17\n";  // ???
    else if (__cplusplus == 201402L) std::cout << "C++14\n";
    else if (__cplusplus == 201103L) std::cout << "C++11\n"; //g++ -std=c++11 check_cpp_version.cpp
    else if (__cplusplus == 199711L) std::cout << "C++98\n"; //g++ check_cpp_version.cpp
    else std::cout << "pre-standard C++\n";
}

Also, below is the output of the following command -

storm@storm:~$ g++ -v --help | grep "std"
Using built-in specs.
COLLECT_GCC=g++
COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-linux-gnu/5/lto-wrapper
Target: x86_64-linux-gnu
:
:
-std=c++17            This switch lacks documentation
-std=c++1y            Deprecated in favor of -std=c++14
-std=c++1z            Conform to the ISO 2017(?) C++ draft standard (experimental and incomplete support)
:
:

The output of the program with compiler syntax

storm@storm:~$ g++ -std=c++1z check_cpp_version.cpp -o check_cpp_version
storm@storm:~$ ./check_cpp_version
storm@storm:~$ pre-standard C++
storm@storm:~$ g++ -std=c++17 check_cpp_version.cpp -o check_cpp_version
storm@storm:~$ ./check_cpp_version
storm@storm:~$ pre-standard C++

I am unable to print out "C++17" for both the cases.

Can anyone please guide me on how to get this thing done? Does it require an update of the compiler(even though it has a -std=c++17 switch) or some other package update or I should move on to a different compiler altogether?

======================================================================== UPDATE TO THIS QUESTION

All I needed to do was to update the gcc/g++ compiler which I did using this link - https://gist.github.com/jlblancoc/99521194aba975286c80f93e47966dc5

You can follow comments and answer to this question to get the understanding.

========================================================================

Upvotes: 1

Views: 1185

Answers (1)

eerorika
eerorika

Reputation: 238401

Important part from the output that you quote:

Conform to the ISO 2017(?) C++ draft standard (experimental and incomplete support)

You are using a compiler which does not support C++17 entirely. The compiler was released in 2015, way before C++17 was finalised.

[__cplusplus] prints out 201500

In that case __cplusplus == 201703L will not be true.

You'll need to use a newer compiler that supports C++17. The latest release version of GCC at the moment of writing is 10.2. I recommend this version.

Upvotes: 3

Related Questions