Namespaces
Variants
Views
Actions

Feature Test Recommendations

From cppreference.com
< cpp
Revision as of 13:12, 24 March 2015 by Bazzy (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

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.

Contents

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.

Finding Headers

__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

Feature Macro name Value Header
New character types (char16_t and char32_t) __cpp_unicode_characters 200704 predefined
Raw and Unicode String Literals __cpp_raw_strings 200710 predefined
__cpp_unicode_literals 200710 predefined
User-defined Literals __cpp_user_defined_literals 200809 predefined
Lambda expressions __cpp_lambdas 200907 predefined
constexpr __cpp_constexpr 200704 predefined
Range-for loop __cpp_range_based_for 200907 predefined
static_assert __cpp_static_assert 200410 predefined
decltype __cpp_decltype 200707 predefined
Forward declaration of enumerations __cpp_forward_decl_enum 200809 predefined
Attributes __cpp_attributes 200809 predefined
Rvalue reference __cpp_rvalue_references 200610 predefined
Variadic templates __cpp_variadic_templates 200704 predefined
Initializer lists __cpp_initializer_lists 200806 predefined
Explicit conversion operators __cpp_explicit_conversion 200710 predefined
Delegating Constructors __cpp_delegating_constructors 200604 predefined
Non-static data member initializers __cpp_nsdmi 200809 predefined
Inheriting Constructors __cpp_inheriting_constructors 200802 predefined
ref-qualifiers __cpp_ref_qualifiers 200710 predefined
Template aliases __cpp_alias_templates 200704 predefined
noexcept __cpp_noexcept 201003 predefined

C++98

Feature Macro name Value Header
Run-time type identification (dynamic_cast, typeid) __cpp_rtti 199711 predefined
Exception handling __cpp_exceptions 199711 predefined

File System TS

Feature Macro name Value Header
Filesystem library __cpp_lib_experimental_filesystem 201406 <experimental/filesystem>

Library Fundamentals TS

Feature Macro Name Value Header
apply() call a function with arguments from a tuple __cpp_lib_experimental_apply 201402 <experimental/tuple>
Variable templates For type traits __cpp_lib_experimental_type_trait_variable_templates 201402 <experimental/type_traits>
Invocation type traits __cpp_lib_experimental_invocation_type 201406 <experimental/type_traits>
Type-erased allocator for std::function __cpp_lib_experimental_function_erased_allocator 201406 <experimental/functional>
Extending std::search to use Additional Searching Algorithms __cpp_lib_experimental_boyer_moore_searching 201411 <experimental/functional>
optional objects __cpp_lib_experimental_optional 201411 <experimental/optional>
class any __cpp_lib_experimental_any 201411 <experimental/any>
string_view: a non-owning reference to a string __cpp_lib_experimental_string_view 201411 <experimental/string_view>
Extending shared_ptr to Support Arrays __cpp_lib_experimental_shared_ptr_arrays 201406 <experimental/memory>
Polymorphic Memory Resources __cpp_lib_experimental_memory_resources 201402 <experimental/memory_resource>
Type-erased allocator for std::promise __cpp_lib_experimental_promise_erased_allocator 201406 <experimental/future>
Type-erased allocator for std::packaged_task __cpp_lib_experimental_packaged_task_erased_allocator 201406 <experimental/future>
sample __cpp_lib_experimental_sample 201402 <experimental/algorithm>

Concepts TS

Feature Macro name Value Header
constraints and concepts __cpp_experimental_concepts 201501 predefined


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

Standing Document 6 The official document on Feature Test Recommendations