When Will Gnu C++ Support C++11 Without Explicitly Asking for It

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.

How do I enable C++11 in gcc?

H2CO3 is right, you can use a makefile with the CXXFLAGS set with -std=c++11
A makefile is a simple text file with instructions about how to compile your program. Create a new file named Makefile (with a capital M). To automatically compile your code just type the make command in a terminal. You may have to install make.

Here's a simple one :

CXX=clang++
CXXFLAGS=-g -std=c++11 -Wall -pedantic
BIN=prog

SRC=$(wildcard *.cpp)
OBJ=$(SRC:%.cpp=%.o)

all: $(OBJ)
$(CXX) -o $(BIN) $^

%.o: %.c
$(CXX) $@ -c $<

clean:
rm -f *.o
rm $(BIN)

It assumes that all the .cpp files are in the same directory as the makefile. But you can easily tweak your makefile to support a src, include and build directories.

Edit : I modified the default c++ compiler, my version of g++ isn't up-to-date. With clang++ this makefile works fine.

gcc using c++11 standard even though 98 explicitly specified

As described here, the libstdc++ v.6 supports new and old ABI. I had to place

-D_GLIBCXX_USE_CXX11_ABI=0

to the Makefile's g++ command. That took care of the lib incompatibility.

Official status of c++11 support in gcc

Looks like actually it's experimental only on words. In gcc 6.0 it will not be experimental more.

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

But now, default mode is gnu++98 and so, it's experimental (one of the reasons, why we don't use C++11/C++14 now).

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.

Why does GCC 6.3 compile this Braced-Init-List code without explicit C++11 support?

The default compiler command for gcc 6.x is -std=gnu++14, so the compiler is implicitly compiling your code using a later version of the C++ language standard.

You will need to manually specify -std=c++03 if you want to compile in C++03.

Why the C standard C11 isn't default in gcc?

The answer is in the page you linked:

GCC supports three versions of the C standard, although support for the most recent version is not yet complete.

Support for C99 is substantially complete, but I think there are a couple of minor things that haven't been implemented yet. According to that page, they intend to make C11 with GNU extensions the default in a future version.

How do I check for C++11 support?

Quick history

When this question was asked in February 2011, support for C++11 was inconsistent. Compilers needed time to catch up to the standard, so in the meantime, they released with partial support. For example, a compiler might have implemented variadic templates, but not auto.

If you had code that depended on a subset of C++11 features, you didn't have a standard way of asking the compiler whether it supported them specifically.

You could check for overall C++11 support with #if __cplusplus >= 201103L, but:

  • That constant and its meaning weren't technically official until the standard was accepted later, in August 2011.
  • This was too coarse. Compilers probably only set __cplusplus to 201103L once they had full feature support—and no one had full feature support yet. So, if you used this, you would unnecessarily reject most or all of the compilers that people would want to use to compile your code.

One pragmatic solution was to use the third-party Boost.Config library, which maintained a bunch of feature test macros. Boost.Config's maintainers kept track of which compilers supported which features, and updated the macros accordingly for each Boost.Config release.

(This stuff about Boost is based on an early, now-deleted answer from @James McNellis.

Today

C++11

Today, there's a version of every major compiler that supports the whole C++11 standard. .

If your code requires any of C++11, it's now reasonable to require that the compiler supports all of C++11. So, use #if __cplusplus >= 201103L. (See @Paulo M's answer for a standards reference for this macro.) But beware that there are complications with MSVC—see @Donald Duck's answer.

Beyond

Since C++20, in addition to the coarse-grained __cplusplus macro, you can also #include <version> to get a bunch of feature test macros. This is basically a standards-based alternative to Boost.Config.

According to this answer from Jarryd, these macros are actually available in at least some 2016-era C++14 compilers.

Availability of C++11 features

You might be interested in feature test macros, which enable you to test for specific C++11, C++14 or even C++17 features, such as __cpp_static_assert, __cpp_lib_make_unique or __cpp_variable_templates. Clang and GCC already support this, see a live demo.

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.



Related Topics



Leave a reply



Submit