Why C++11 Compiler Support Still Requires a Flag

Why C++11 compiler support still requires a flag?

C++11 has been standard for a couple of years, but a compiler isn't going to switch its default mode to C++11 until:

  • At an absolute minimum, C++11 support is complete in that compiler and the libraries it uses. And also stable, if the compiler writer has any concern at all for reliability.
  • Preferably, a major version number increase in the compiler, since C++11 is not fully backward-compatible to C++03.
  • Ideally, on a well-known schedule so that users can prepare for the change.

Basically, lots of people (and makefiles) rely on the compiler being a conforming C++03 compiler, or at least on its non-conformance being known. Since C++11 introduces new instances of non-conformance with C++03, the change is potentially traumatic.

Arguably anyone relying on C++03 should have specified an option to say so, and changes to the default mode would make no difference to them. But once you've documented your compiler's default, people are going to rely that, either intentionally or otherwise.

For gcc in particular, the 4.8.2 man page says that "support for C++11 is still experimental". So I think ultimately the answer to your question may be that it takes more than 2 years to properly implement C++11, even starting from all the work done with draft standards.

Do I need -pedantic flag from GCC with C11?

If you need it, you need it. If you don't, you don't.

gcc's -pedantic option tells it to strictly enforce the rules of the C standard you've requested. This results in additional warning messages (or fatal errors if you use -pedantic-errors).

The question is, do you want the compiler to warn you about code that violates the requirements of the C standard?

If you want your code to be as portable as possible, use -pedantic and pay close attention to anything it tells you. If you want your code to depend on non-standard features, don't use -pedantic -- but then you run the risk that your code might not compile with a different compiler and/or for a different target system.

The specific messages you're run into are for things that have changed between C90 and C11. C11 requires compilers to support at least 4095 characters in a string literal; C90 only requires 509. (In practice, for gcc, the actual limit is not fixed but is imposed by available memory at compile time. And the way the limits are described in the standard is not that simple, but I won't get into that.) Still, you'll rarely need to have a string literal that long.

C99 added the %zu format for printing a value of type size_t. If you want your code to be portable to pre-C99 implementations, you'll need to avoid using it; for example, you can use printf("%lu\n", (unsigned long)sizeof foo). In practice, most current implementations do support %zu.

A return statement with an expression, even an expression of type void, is not permitted in a function defined with a void return type. That's a warning you should want (IMHO).

Bottom line: Use -pedantic if you want to strictly enforce the rules of the C standard. If you don't want to do that, don't use -pedantic. (But consider compiling your code with -pedantic at least occasionally to weed out any real errors it detects, in addition to warnings that you might not care about.)

Compiling C++11 with g++

Flags (or compiler options) are nothing but ordinary command line arguments passed to the compiler executable.

Assuming you are invoking g++ from the command line (terminal):

$ g++ -std=c++11 your_file.cpp -o your_program

or

$ g++ -std=c++0x your_file.cpp -o your_program

if the above doesn't work.

When will Gnu C++ support C++11 without explicitly asking for it?

GCC 6.0: https://gcc.gnu.org/gcc-6/changes.html

The default mode for C++ is now -std=gnu++14 instead of -std=gnu++98.

Regex in C++: Requires compiler support error

and must be enabled with the -std=c++0x or -std=gnu++0x compiler options

Add one of those options, -std=c++0x or -std=gnu++0x, to your compiler command:

g++ -std=c++0x ...

Note if std::regex is not supported see boost::regex for an alternative.

C++11 experimental functions

Once I add. '-std=c++11' to the compiler options I no longer get the error/warning message but I was already aware of that. I was puzzled as to why it did give me a warning without adding it. Shouldn't the current std on GCC be C++11 already? Why is this still not the case?

Software and build systems out there use the defaults and expect them to remain fairly stable. GCC does not simply update the default settings every time new standards or features are available.

If you want a particular version of the standard then you should specify it explicitly.

configure error A compiler with support for C++11 language features is required.

While GCC 4.7 does have some C++11 capabilities, it is severely lacking most of the features needed. So while the configure script uses the correct option to enable C++11, the compiler can't actually handle the test-program because it uses features not available in the old GCC 4.7 version you have.

If you want to use Breakpad you need either an older version of Breakpad that supports your old compiler, or you need to update your compiler to a more recent release. The 5 series should have full support for C++11.

Need to enable C++11 in Codeblocks

You could always just select the -std=c++0x option, but another solution is to install more recent mingw version since the problem is obviously the compiler, not Code::Blocks



Related Topics



Leave a reply



Submit