Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
josuttis_nm_c20_the_complete_guide.pdf
Скачиваний:
44
Добавлен:
27.03.2023
Размер:
5.85 Mб
Скачать

21.7 Feature Test Macros

671

21.7 Feature Test Macros

Each and every C++ version introduces various language and library features that are supported by compilers step by step. For that reason, knowing which C++ version a compiler supports in general is often not enough; for portable code, it might be important to know whether a specific feature is available.

For that purpose, C++20 officially introduces feature test macros. For each new language and library feature, a macro can be used to signal whether the feature is available. The macro can even provide information about which version of a feature is supported.

For example, the following source code will use different code depending on whether (and in which form) generic lambdas are available:

#ifdef __cpp_generic_lambdas

#if __cpp_generic_lambdas >= 201707

... // generic lambdas with template parameters can be used

#else

... // generic lambdas can be used

#else

... // no generic lambdas can be used

#endif

All feature test macros for language features start with __cpp.

As another example, the following code provides and uses a workaround if std::as_const() is not available yet:

#ifndef __cpp_lib_as_const template<typename T> const T& asConst(T& t) {

return t;

}

#endif

#ifdef __cpp_lib_as_const

auto printColl = [&coll = std::as_const(coll)] { #else

auto printColl = [&coll = asConst(coll)] { #endif

...

};

All feature test macros for library features start with __cpp_lib.

Feature test macros for language features are defined by the compilers. Feature test macros for library features are provided by the new header <version>.

See the use of __cpp_char8_t as another example of making code that uses UTF-8 characters portable before and after C++20.

672

Chapter 21: Small Improvements for the Core Language

21.8Afternotes

The range-based for loop with initialization was first proposed by Thomas Koppe¨ in http://wg21. link/p0614r0. The finally accepted wording was formulated by Thomas Koppe¨ in http://wg21.link/ p0614r1.

using for enumeration values was first proposed by Gasper Azman and Jonathan Muller¨ in http:// wg21.link/p1099r0. The finally accepted wording was formulated by Gasper Azman and Jonathan Muller¨ in http://wg21.link/p1099r5.

The request for a distinct type for UTF-8 characters was first proposed by Tom Honermann in http: //wg21.link/p0482r0. The finally accepted wording was formulated by Tom Honermann in http:// wg21.link/p0482r6. The corresponding fixes to the output operators were accepted as proposed by Tom Honermann in http://wg21.link/p1423r3.

The request to support designated initializers was first proposed by Tim Shen, Richard Smith, Zhihao Yuan, and Chandler Carruth in http://wg21.link/p0329r0. The finally accepted wording was formulated by Tim Shen and Richard Smith in http://wg21.link/p0329r4.

Aggregate initialization with parentheses was first proposed by Ville Voutilainen in http://wg21.link/ p0960r0. The finally accepted wording was formulated by Ville Voutilainen and Thomas Koppe¨ in http: //wg21.link/p0960r3.

The change of the definition of an aggregate was accepted as proposed by Timur Doumler, Arthur O’Dwyer, Richard Smith, Howard E. Hinnant, and Nicolai Josuttis in http://wg21.link/p1008r1.

The attributes [[likely]] and [[unlikely]] were accepted as proposed by Clay Trychta in http: //wg21.link/p0479r5.

The attribute [[no_unique_address]] was accepted as proposed by Richard Smith in http://wg21. link/p0840r2.

Allowing the attribute [[nodiscard]] to have an argument was accepted as proposed by JeanHeyd Meneide and Isabella Muerte in http://wg21.link/p1301r4.

Feature test macros were adopted into C++ as proposed by Ville Voutilainen and Jonathan Wakely in http://wg21.link/p0941r2.