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.