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












8















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










share|improve this question

























  • Setting only a single file is not a good idea.

    – StoryTeller
    Nov 15 '18 at 14:05













  • What's wrong with adjusting compiler settings as you described?

    – HolyBlackCat
    Nov 15 '18 at 14:05











  • Such a #define (or #pragma) wouldn't make sense. It might appear in the middle of source code. What to do in this case? Changing the standard of C++ in the middle of source code. (In edge cases, this can cause even slight changes of grammar.)

    – Scheff
    Nov 15 '18 at 14:07








  • 1





    Changing #define __cplusplus is not only not a good idea, it's also illegal since that is a reserved identifier.

    – Eljay
    Nov 15 '18 at 14:08











  • @HolyBlackCat Because Global Compiler setting. If I were to compile a single file (Not project), and I set the compiler option, It's set to to global, and other files that I open will follow c++1 And also, When my friend copies the file, he'll get confused why the code gets error, because he don't understand how to set Compile Option

    – Sazeim Saheem
    Nov 15 '18 at 14:10
















8















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










share|improve this question

























  • Setting only a single file is not a good idea.

    – StoryTeller
    Nov 15 '18 at 14:05













  • What's wrong with adjusting compiler settings as you described?

    – HolyBlackCat
    Nov 15 '18 at 14:05











  • Such a #define (or #pragma) wouldn't make sense. It might appear in the middle of source code. What to do in this case? Changing the standard of C++ in the middle of source code. (In edge cases, this can cause even slight changes of grammar.)

    – Scheff
    Nov 15 '18 at 14:07








  • 1





    Changing #define __cplusplus is not only not a good idea, it's also illegal since that is a reserved identifier.

    – Eljay
    Nov 15 '18 at 14:08











  • @HolyBlackCat Because Global Compiler setting. If I were to compile a single file (Not project), and I set the compiler option, It's set to to global, and other files that I open will follow c++1 And also, When my friend copies the file, he'll get confused why the code gets error, because he don't understand how to set Compile Option

    – Sazeim Saheem
    Nov 15 '18 at 14:10














8












8








8








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










share|improve this question
















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







c++ c++11 g++ preprocessor-directive






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 15 '18 at 14:17







Sazeim Saheem

















asked Nov 15 '18 at 14:04









Sazeim SaheemSazeim Saheem

6411




6411













  • Setting only a single file is not a good idea.

    – StoryTeller
    Nov 15 '18 at 14:05













  • What's wrong with adjusting compiler settings as you described?

    – HolyBlackCat
    Nov 15 '18 at 14:05











  • Such a #define (or #pragma) wouldn't make sense. It might appear in the middle of source code. What to do in this case? Changing the standard of C++ in the middle of source code. (In edge cases, this can cause even slight changes of grammar.)

    – Scheff
    Nov 15 '18 at 14:07








  • 1





    Changing #define __cplusplus is not only not a good idea, it's also illegal since that is a reserved identifier.

    – Eljay
    Nov 15 '18 at 14:08











  • @HolyBlackCat Because Global Compiler setting. If I were to compile a single file (Not project), and I set the compiler option, It's set to to global, and other files that I open will follow c++1 And also, When my friend copies the file, he'll get confused why the code gets error, because he don't understand how to set Compile Option

    – Sazeim Saheem
    Nov 15 '18 at 14:10



















  • Setting only a single file is not a good idea.

    – StoryTeller
    Nov 15 '18 at 14:05













  • What's wrong with adjusting compiler settings as you described?

    – HolyBlackCat
    Nov 15 '18 at 14:05











  • Such a #define (or #pragma) wouldn't make sense. It might appear in the middle of source code. What to do in this case? Changing the standard of C++ in the middle of source code. (In edge cases, this can cause even slight changes of grammar.)

    – Scheff
    Nov 15 '18 at 14:07








  • 1





    Changing #define __cplusplus is not only not a good idea, it's also illegal since that is a reserved identifier.

    – Eljay
    Nov 15 '18 at 14:08











  • @HolyBlackCat Because Global Compiler setting. If I were to compile a single file (Not project), and I set the compiler option, It's set to to global, and other files that I open will follow c++1 And also, When my friend copies the file, he'll get confused why the code gets error, because he don't understand how to set Compile Option

    – Sazeim Saheem
    Nov 15 '18 at 14:10

















Setting only a single file is not a good idea.

– StoryTeller
Nov 15 '18 at 14:05







Setting only a single file is not a good idea.

– StoryTeller
Nov 15 '18 at 14:05















What's wrong with adjusting compiler settings as you described?

– HolyBlackCat
Nov 15 '18 at 14:05





What's wrong with adjusting compiler settings as you described?

– HolyBlackCat
Nov 15 '18 at 14:05













Such a #define (or #pragma) wouldn't make sense. It might appear in the middle of source code. What to do in this case? Changing the standard of C++ in the middle of source code. (In edge cases, this can cause even slight changes of grammar.)

– Scheff
Nov 15 '18 at 14:07







Such a #define (or #pragma) wouldn't make sense. It might appear in the middle of source code. What to do in this case? Changing the standard of C++ in the middle of source code. (In edge cases, this can cause even slight changes of grammar.)

– Scheff
Nov 15 '18 at 14:07






1




1





Changing #define __cplusplus is not only not a good idea, it's also illegal since that is a reserved identifier.

– Eljay
Nov 15 '18 at 14:08





Changing #define __cplusplus is not only not a good idea, it's also illegal since that is a reserved identifier.

– Eljay
Nov 15 '18 at 14:08













@HolyBlackCat Because Global Compiler setting. If I were to compile a single file (Not project), and I set the compiler option, It's set to to global, and other files that I open will follow c++1 And also, When my friend copies the file, he'll get confused why the code gets error, because he don't understand how to set Compile Option

– Sazeim Saheem
Nov 15 '18 at 14:10





@HolyBlackCat Because Global Compiler setting. If I were to compile a single file (Not project), and I set the compiler option, It's set to to global, and other files that I open will follow c++1 And also, When my friend copies the file, he'll get confused why the code gets error, because he don't understand how to set Compile Option

– Sazeim Saheem
Nov 15 '18 at 14:10












3 Answers
3






active

oldest

votes


















6














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.






share|improve this answer
























  • I thought it had to be quoted but just found Error directive: error_message can consist of several words not necessarily in quotes.

    – Scheff
    Nov 15 '18 at 14:17



















3















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.






share|improve this answer

































    1














    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..






    share|improve this answer


























    • Imagine the possibilities! #define -std Pascal ... #define -std Ada ... #define -std C++1985 ... #define -std C++17` ... #define -std VB.NET. What could go wnorg?

      – Eljay
      Nov 15 '18 at 14:11






    • 2





      @Eljay it would keep things...spicy!

      – gsamaras
      Nov 15 '18 at 14:11











    • If it was a project, YES! It'll be a nightmare, but this is a single build file, so It shouldn't be any problem.

      – Sazeim Saheem
      Nov 15 '18 at 14:19






    • 1





      @SazeimSaheem I see your point, but in any case, it's not possible. You asked a good question, you deserved my upvote! Cheers

      – gsamaras
      Nov 15 '18 at 14:23











    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53321215%2fis-it-possible-to-set-g-to-follow-c11-iso-std-c11-through-define%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    3 Answers
    3






    active

    oldest

    votes








    3 Answers
    3






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    6














    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.






    share|improve this answer
























    • I thought it had to be quoted but just found Error directive: error_message can consist of several words not necessarily in quotes.

      – Scheff
      Nov 15 '18 at 14:17
















    6














    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.






    share|improve this answer
























    • I thought it had to be quoted but just found Error directive: error_message can consist of several words not necessarily in quotes.

      – Scheff
      Nov 15 '18 at 14:17














    6












    6








    6







    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.






    share|improve this answer













    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.







    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Nov 15 '18 at 14:14









    Toby SpeightToby Speight

    17k134266




    17k134266













    • I thought it had to be quoted but just found Error directive: error_message can consist of several words not necessarily in quotes.

      – Scheff
      Nov 15 '18 at 14:17



















    • I thought it had to be quoted but just found Error directive: error_message can consist of several words not necessarily in quotes.

      – Scheff
      Nov 15 '18 at 14:17

















    I thought it had to be quoted but just found Error directive: error_message can consist of several words not necessarily in quotes.

    – Scheff
    Nov 15 '18 at 14:17





    I thought it had to be quoted but just found Error directive: error_message can consist of several words not necessarily in quotes.

    – Scheff
    Nov 15 '18 at 14:17













    3















    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.






    share|improve this answer






























      3















      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.






      share|improve this answer




























        3












        3








        3








        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.






        share|improve this answer
















        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.







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 15 '18 at 14:12









        gsamaras

        51.8k24105190




        51.8k24105190










        answered Nov 15 '18 at 14:05









        YSCYSC

        24.8k557112




        24.8k557112























            1














            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..






            share|improve this answer


























            • Imagine the possibilities! #define -std Pascal ... #define -std Ada ... #define -std C++1985 ... #define -std C++17` ... #define -std VB.NET. What could go wnorg?

              – Eljay
              Nov 15 '18 at 14:11






            • 2





              @Eljay it would keep things...spicy!

              – gsamaras
              Nov 15 '18 at 14:11











            • If it was a project, YES! It'll be a nightmare, but this is a single build file, so It shouldn't be any problem.

              – Sazeim Saheem
              Nov 15 '18 at 14:19






            • 1





              @SazeimSaheem I see your point, but in any case, it's not possible. You asked a good question, you deserved my upvote! Cheers

              – gsamaras
              Nov 15 '18 at 14:23
















            1














            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..






            share|improve this answer


























            • Imagine the possibilities! #define -std Pascal ... #define -std Ada ... #define -std C++1985 ... #define -std C++17` ... #define -std VB.NET. What could go wnorg?

              – Eljay
              Nov 15 '18 at 14:11






            • 2





              @Eljay it would keep things...spicy!

              – gsamaras
              Nov 15 '18 at 14:11











            • If it was a project, YES! It'll be a nightmare, but this is a single build file, so It shouldn't be any problem.

              – Sazeim Saheem
              Nov 15 '18 at 14:19






            • 1





              @SazeimSaheem I see your point, but in any case, it's not possible. You asked a good question, you deserved my upvote! Cheers

              – gsamaras
              Nov 15 '18 at 14:23














            1












            1








            1







            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..






            share|improve this answer















            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..







            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Nov 15 '18 at 14:11

























            answered Nov 15 '18 at 14:05









            gsamarasgsamaras

            51.8k24105190




            51.8k24105190













            • Imagine the possibilities! #define -std Pascal ... #define -std Ada ... #define -std C++1985 ... #define -std C++17` ... #define -std VB.NET. What could go wnorg?

              – Eljay
              Nov 15 '18 at 14:11






            • 2





              @Eljay it would keep things...spicy!

              – gsamaras
              Nov 15 '18 at 14:11











            • If it was a project, YES! It'll be a nightmare, but this is a single build file, so It shouldn't be any problem.

              – Sazeim Saheem
              Nov 15 '18 at 14:19






            • 1





              @SazeimSaheem I see your point, but in any case, it's not possible. You asked a good question, you deserved my upvote! Cheers

              – gsamaras
              Nov 15 '18 at 14:23



















            • Imagine the possibilities! #define -std Pascal ... #define -std Ada ... #define -std C++1985 ... #define -std C++17` ... #define -std VB.NET. What could go wnorg?

              – Eljay
              Nov 15 '18 at 14:11






            • 2





              @Eljay it would keep things...spicy!

              – gsamaras
              Nov 15 '18 at 14:11











            • If it was a project, YES! It'll be a nightmare, but this is a single build file, so It shouldn't be any problem.

              – Sazeim Saheem
              Nov 15 '18 at 14:19






            • 1





              @SazeimSaheem I see your point, but in any case, it's not possible. You asked a good question, you deserved my upvote! Cheers

              – gsamaras
              Nov 15 '18 at 14:23

















            Imagine the possibilities! #define -std Pascal ... #define -std Ada ... #define -std C++1985 ... #define -std C++17` ... #define -std VB.NET. What could go wnorg?

            – Eljay
            Nov 15 '18 at 14:11





            Imagine the possibilities! #define -std Pascal ... #define -std Ada ... #define -std C++1985 ... #define -std C++17` ... #define -std VB.NET. What could go wnorg?

            – Eljay
            Nov 15 '18 at 14:11




            2




            2





            @Eljay it would keep things...spicy!

            – gsamaras
            Nov 15 '18 at 14:11





            @Eljay it would keep things...spicy!

            – gsamaras
            Nov 15 '18 at 14:11













            If it was a project, YES! It'll be a nightmare, but this is a single build file, so It shouldn't be any problem.

            – Sazeim Saheem
            Nov 15 '18 at 14:19





            If it was a project, YES! It'll be a nightmare, but this is a single build file, so It shouldn't be any problem.

            – Sazeim Saheem
            Nov 15 '18 at 14:19




            1




            1





            @SazeimSaheem I see your point, but in any case, it's not possible. You asked a good question, you deserved my upvote! Cheers

            – gsamaras
            Nov 15 '18 at 14:23





            @SazeimSaheem I see your point, but in any case, it's not possible. You asked a good question, you deserved my upvote! Cheers

            – gsamaras
            Nov 15 '18 at 14:23


















            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53321215%2fis-it-possible-to-set-g-to-follow-c11-iso-std-c11-through-define%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Bressuire

            Vorschmack

            Quarantine