To track progress of partial implementation of experimantal features, the feature test recommendations provide a set of preprocessor macros which, if defined by the implementation, give a simple and portable way to detect the presence of said features.
Function Macros
Feature test function macros can be expanded in the expression of #if and #elif.
They will be treated as defined macros by #ifdef, #ifndef and defined but cannot be used anywhere else.
|
__has_include( filename )
|
(1)
|
|
|
__has_include( < filename> )
|
(2)
|
|
|
__has_include( " filename" )
|
(3)
|
|
|
If the header is found, it expands to 1
, 0
otherwise.
1) filename is treated as a preprocessor token and expanded as usual before checking
2,3) Perform a check on the header file name in the same way a
#include directive would interpret it.
Attributes
|
__has_cpp_attribute( attribute-token )
|
|
|
|
Checks for the presence of an attribute.
For standard attributes, it will expand to the year and month in which the attribute was added to the working draft, the presence of vendor-specific attributes is determined by a non-zero value.
Language Features
The following macros expand to a numeric value corresponding to the year and month when the feature has been included in the working draft.
When a feature changes significantly, the macro will be updated accordingly.
C++14
Feature
|
Macro name
|
Value
|
Header
|
Binary Literals in the C++ Core Language
|
__cpp_binary_literals
|
201304
|
predefined
|
Single-Quotation-Mark as a Digit Separator
|
__cpp_digit_separators
|
201309
|
predefined
|
Wording Changes for Generalized Lambda-capture
|
__cpp_init_captures
|
201304
|
predefined
|
Generic (Polymorphic) Lambda Expressions
|
__cpp_generic_lambdas
|
201304
|
predefined
|
C++ Sized Deallocation
|
__cpp_sized_deallocation
|
201309
|
predefined
|
Relaxing constraints on constexpr functions / constexpr member functions and implicit const
|
__cpp_constexpr
|
201304
|
predefined
|
Return type deduction for normal functions
|
__cpp_decltype_auto
|
201304
|
predefined
|
Return type deduction for normal functions
|
__cpp_return_type_deduction
|
201304
|
predefined
|
Member initializers and aggregates
|
__cpp_aggregate_nsdmi
|
201304
|
predefined
|
Variable Templates
|
__cpp_variable_templates
|
201304
|
predefined
|
Compile-time integer sequences
|
__cpp_lib_integer_sequence
|
201304
|
<utility>
|
exchange() utility function
|
__cpp_lib_exchange_function
|
201304
|
<utility>
|
Wording for Addressing Tuples by Type
|
__cpp_lib_tuples_by_type
|
201304
|
<utility>
|
Consistent Metafunction Aliases
|
__cpp_lib_tuple_element_t
|
201402
|
<utility>
|
make_unique
|
__cpp_lib_make_unique
|
201304
|
<memory>
|
Making Operator Functors greater<>
|
__cpp_lib_transparent_operators
|
201210
|
<functional>
|
std::result_of and SFINAE
|
__cpp_lib_result_of_sfinae
|
201210
|
<functional>
|
An Incremental Improvement to integral_constant
|
__cpp_lib_integral_constant_callable
|
201304
|
<type_traits>
|
TransformationTraits Redux
|
__cpp_lib_transformation_trait_aliases
|
201304
|
<type_traits>
|
User-defined classes that cannot be derived from
|
__cpp_lib_is_final
|
201402
|
<type_traits>
|
Type traits and std::nullptr_t
|
__cpp_lib_is_null_pointer
|
201309
|
<type_traits>
|
User-defined Literals for Time Types
|
__cpp_lib_chrono_udls
|
201304
|
<chrono>
|
User-defined Literals for String Types
|
__cpp_lib_string_udls
|
201304
|
<string>
|
Adding heterogeneous comparison lookup to associative containers
|
__cpp_lib_generic_associative_lookup
|
201304
|
<map> <set>
|
Null Forward Iterators
|
__cpp_lib_null_iterators
|
201304
|
<iterator>
|
make_reverse_iterator
|
__cpp_lib_make_reverse_iterator
|
201402
|
<iterator>
|
Making non-modifying sequence operations more robust
|
__cpp_lib_robust_nonmodifying_seq_ops
|
201304
|
<algorithm>
|
User-defined Literals for std::complex
|
__cpp_lib_complex_udls
|
201309
|
<complex>
|
Quoted Strings Library Proposal
|
__cpp_lib_quoted_string_io
|
201304
|
<iomanip>
|
A proposal to rename shared_mutex to shared_timed_mutex
|
__cpp_lib_shared_timed_mutex
|
201402
|
<shared_mutex>
|
C++11
C++98
File System TS
Library Fundamentals TS
Concepts TS
Example
#ifdef __has_include // Check if __has_include is present
# if __has_include(<optional>) // Check for a standard library
# include<optional>
# elif __has_include(<experimental/optional>) // Check for an experimental version
# include <experimental/optional>
# elif __has_include(<boost/optional.hpp>) // Try with an external library
# include <boost/optional.hpp>
# else // Not found at all
# error "Missing <optional>"
# endif
#endif
#ifdef __has_cpp_attribute // Check if __has_cpp_attribute is present
# if __has_cpp_attribute(deprecated) // Check for an attribute
# define DEPRECATED(msg) [[deprecated(msg)]]
# endif
#endif
#ifndef DEPRECATED
# define DEPRECATED(msg)
#endif
DEPRECATED("foo() has been deprecated") void foo();
#if __cpp_constexpr >= 201304 // Check for a specific version of a feature
# define CONSTEXPR constexpr
#else
# define CONSTEXPR
#endif
CONSTEXPR int bar(unsigned i)
{
#ifdef __cpp_binary_literals // Check for the presence of a feature
unsigned mask1 = 0b11000000;
unsigned mask2 = 0b00000111;
#else
unsigned mask1 = 0xA0;
unsigned mask2 = 0x07;
#endif
if ( i & mask1 )
return 1;
if ( i & mask2 )
return 2;
return 0;
}
int main()
{
}
See Also