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
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With