Reputation: 386
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
Upvotes: 8
Views: 864
Reputation: 30871
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.
Upvotes: 6
Reputation: 40100
Is it possible to set g++ to follow C++11 ISO (
-std=c++11
) through#define
?
No.
Neither C++ nor g++ have that feature. You might want to build simple one-file programs manually.
Upvotes: 3
Reputation: 73394
No.
You shouldn't changing the #define __cplusplus
. Read more in How to trigger the __cplusplus (C++) #ifdef?, because __cplusplus
should be automatically defined by C++ compiler. That's why changing the version is meant to be done via the compiler settings.
It doesn't make sense to have a file follow C++11, while the others would follow C++14, for example. The whole project should be compiled in a homogeneous way.
Notice that your code wouldn't compile: Error: macro names must be identifiers using #ifdef 0.
PS: What a nightmare it would be in terms of readability and maintenance if what you described was a good idea..
Upvotes: 1