Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to set g++ to follow C++11 ISO (-std=c++11) through #define?

I'm quite new to c++11 and I was wondering something...

I am using Code::Blocks and if I were to use c++11 in this IDE, i had to go to compiler settings, and and Check "Have g++ follow the C++11 ISO C++ language standard"

Is there any workaround so I can set a single .cpp file to use c++11 in the #define statement like this?

Note: This is a single "Build" file, NOT a project

By setting the compile option while not in project, it'll set it to Global Compile option that I prefer to not happen

I know that you can customize the build option in Project Files that It'll set c++11 for that project only

#include <iostream>

#define -std c++11

int main(){

    #if __cplusplus==201402L
        std::cout << "C++14" << std::endl;
    #elif __cplusplus==201103L
        std::cout << "C++11" << std::endl;
    #else
        std::cout << "C++" << std::endl;
    #endif
    return 0;
}

What I have found:

Changing #define __cplusplus 201103L is NOT a good idea, because it don't set the compiler to compile as c++11

like image 314
Zombie Chibi XD Avatar asked Nov 15 '18 14:11

Zombie Chibi XD


People also ask

How do I know if G ++ supports C++11?

To see if your compiler has C++11 support, run it with just the --version option to get a print out of the version number. Do this for whichever compiler(s) you wish to use with Rosetta. Acceptable versions: GCC/g++: Version 4.8 or later.

How do I enable C ++ 17 in G ++?

Select the latest version from this list. For GCC/G++, you can pass compiler flags -std=c++11, -std=c++14, -std=c++17, or -std=c++20 to enable C++11/14/17/20 support respectively. If you have GCC 8 or 9, you'll need to use -std=c++2a for C++20 support instead.

Does G ++ support C ++ 14?

The C++ compiler of GCC is known as g++ . The g++ utility supports almost all mainstream C++ standards, including c++98 , c++03 , c++11 , c++14 , c++17 , and experimentally c++20 and c++23 . It also provides some GNU extensions to the standard to enable more useful features.


1 Answers

Although I can see how it would be desirable for a source file to be self-documenting in this respect, this isn't possible.

The next best thing is to test conformance, as you've started to do:

#if __cplusplus < 201103L
#error This source must be compiled as C++11 or later
#endif

That ensures that compilation with a C++03 compiler will give a simple, understandable error message straight away.

like image 101
Toby Speight Avatar answered Nov 10 '22 06:11

Toby Speight