0

While trying to compile a simple range based for loop on MacOS Big Sur, I got this warning:

warning: range-based for loop is a C++11 extension [-Wc++11-extensions]

I tried using clang++ and g++ but both gave the same warning. Is there a way to always compile with C++11 without having to use -std=c++11 and without using aliases?

Edit: The reason I would prefer not to use -std=c++11 is because I want the compiler to default to C++11 or higher.

  • 5
    Well, range loop is C++11 feature, so no. What's wrong with `-std=c++11`? – Quimby Aug 17 '21 at 20:57
  • 2
    "_without having to use `-std=c++11`_" - Why? If your compiler(s) defaults to a C++ version that you don't want to use, why not specify the version you do want to use? – Ted Lyngmo Aug 17 '21 at 20:58
  • 4
    *Is there a way to always compile with C++11 without having to use -std=c++11 and without using aliases?* No. Don't rely on the compiler to pick the right options for you. If you don't like typing in all of the options manually, get a build system to do it for you like CMAKE or and IDE like VS code, Eclipse, or XCode – NathanOliver Aug 17 '21 at 20:59
  • It may be useful to look at [Which C++ standard is the default when compiling with g++?](https://stackoverflow.com/q/44734397/10077). – Fred Larson Aug 17 '21 at 21:00
  • 1
    macOS system clang++ defaults to C++98. It is fairly modern, in that I believe C++17 is fully supported. – sweenish Aug 17 '21 at 21:01
  • 3
    if you are typing `g++ myprogram.cpp` at the command line, you're doing something wrong anyway. You will need to specify the output program name, and turn on either debug information or optimization level, and of course '-Wall'. Put the `-std=c++11` flag in your build system with the others, or at the very least in your `build.sh` and get back to programming. – Peter Aug 17 '21 at 21:02
  • I don't believe there is, short of modifying the compiler's source and building it yourself. I also fail to see the purpose, as you'd still need to specify a different version explicitly for anything else than C++11. Code and compilation options belong together. Build systems are there for a reason. – Yun Aug 17 '21 at 21:10
  • @Yun you would make the code no longer standard conforming, so it would only work on that patched compiler version you've made... Which means you have to do all the updates yourself, etc... – Alex Huszagh Aug 17 '21 at 21:16
  • @AlexanderHuszagh Not if the "default language/version" would be the only change. Code shouldn't depend on one compiler's default language/version anyway. And no, it's not a practical solution, but it's the only way I see that accomplishes what lostinparadise asked for. – Yun Aug 17 '21 at 22:09
  • @Yun You're right, code shouldn't depend on the compilers version. I will stick to using `-std=c++11`. Thanks. – lostinparadise Aug 17 '21 at 22:17
  • @NathanOliver I wasn't aware that I shouldn't rely on the compiler, thanks for letting me know. I think I asked a bad question anyway, I'm still learning about the intricacies of C++ and didn't see a good answer to this anywhere else so I asked, but it seems like the general consensus is that I should be using `-std=c++11`, which I'll start doing now. – lostinparadise Aug 17 '21 at 22:22
  • C++11 is quite old now. Specifying (and using) a later standard might be more appropriate, since your compiler supports it. – Paul Sanders Aug 17 '21 at 22:35

1 Answers1

0

To provide this question with a proper answer, based on the discussion in the comments:

Compilers such as GCC and Clang set the default in their source code and it cannot be changed by, e.g., modifying a config file. The only way to change the default would be to change it in the source code and to compile the compiler yourself. This is not worth it.

Furthermore, compilers change their default language from time to time, and setting another default, e.g. to C++11, will make all non-C++11 code require setting the language version explicitly.

Here's the key point: code and compilation options belong together. Do not rely on compiler defaults. Any serious project will use a build system (e.g. Make) which specifies how to compile the project.

Edit

For completeness sake, the default C++ version for GCC 10.2.0 is hardcoded in /gcc/c-family/c-opts.c:

/* Set C++ standard to C++17 if not specified on the command line.  */
if (c_dialect_cxx ())
  set_std_cxx17 (/*ISO*/false);
Yun
  • 3,056
  • 6
  • 9
  • 28
  • This isn't helpful, since if you already have the compiler, you can change the compiler standard version. Changing the source code is a very roundabout way of going around this, and the code will now be non-conformant and therefore will likely not work on any other compiler. If you're tying yourself to a specific compiler, you can add a compiler flag for C++11 support without any issues of ABI compatibility as long as you're using a sufficiently modern compiler version (ABI broke after GCC 4.7, which is the first to support C++11). – Alex Huszagh Aug 18 '21 at 16:09
  • Or, tl;dr: If you're already using a specific compiler and version that supports C++11, just pass the flag. Your code will not compile on any compiler that does not support C++11. In short, this isn't just "not worth it", it's a terrible idea. – Alex Huszagh Aug 18 '21 at 16:10
  • @AlexanderHuszagh I'm not sure if I understand your point. lostinparadise asks how to change a compiler's default language/version to C++11, so that they won't have to pass a flag or use an alias (or build system). My answer is that this can only be done by modifying, e.g., GCC's source. I'm _not_ saying that this is a good idea - quite the opposite. – Yun Aug 18 '21 at 17:03
  • They asked specifically "Is there a way to always compile with C++11 without having to use -std=c++11 and without using aliases?", and mentioned multiple compilers. Modifying the source code only applies to **1 compiler** and only works **if** the compiler already supports C++11. In short, modifying the source code is irrelevant unless you backport the functionality: the only answer is **you cannot**. – Alex Huszagh Aug 18 '21 at 17:37
  • The correct answer would be to say **you cannot** and then highlighting how to do this in various build tools, such as CMake or Meson. – Alex Huszagh Aug 18 '21 at 17:38
  • @AlexanderHuszagh They mentioned _trying_ multiple compilers, but don't require their solution to work with multiple compilers. Of course the compiler needs to already support it. Of course backporting is necessary. We seem to agree that, theoretically, it _can_ be done, but that it's not a practical solution. My answer explains this and suggests an alternative. I'd suggest that you submit your own answer. – Yun Aug 18 '21 at 17:49