C++17 idioms and their C++20 spellings
The C++17 way of writing something, the C++20 name for the same thing, and the feature-test macro that says whether the toolchain has it. Test the macro, not the compiler version: library features arrive at different times in libstdc++, libc++ and MSVC.
| C++17 idiom | C++20 spelling | Feature-test macro |
|---|---|---|
a std::thread joined by hand or in a destructor |
std::jthread, std::stop_token |
__cpp_lib_jthread |
| the erase-remove idiom | std::erase_if |
__cpp_lib_erase_if |
m.find(k) != m.end() |
m.contains(k) |
none; test __cplusplus >= 202002L (_MSVC_LANG on MSVC) |
streams and snprintf |
std::format |
__cpp_lib_format |
| a pointer plus a length | std::span |
__cpp_lib_span |
find_if with a lambda, the map-to-vector dance |
ranges and views | __cpp_lib_ranges |
the detection idiom, static_assert on a trait |
concepts and requires |
__cpp_concepts |
__FILE__ and __LINE__ through a macro |
std::source_location |
__cpp_lib_source_location |
Feature-test macros live in <version>; include it before testing any __cpp_lib_* name.
Source: https://mkhomutov.github.io/going-unmanaged/K-the-standards-catalogue/