Feature-test macros are a powerful way to enable or disable code based on the support of specific C++ features and standards in your compiler. For C++14, the feature-test macros allow you to check if your compiler supports certain features introduced in this version of the C++ standard.
To use feature-test macros for C++14, you can utilize the following predefined macros:
__cplusplus
- This macro is defined to indicate the version of the C++ standard being used. For C++14, it is defined as 201402L
.__cpp_rvalue_references
- Checks support for rvalue references.__cpp_binary_literals
- Checks support for binary literals.__cpp_generic_lambdas
- Checks support for generic lambdas.__cpp_variable_templates
- Checks support for variable templates.Here is an example of how to use feature-test macros to conditionally compile code based on C++14 features:
#include
#if __cplusplus >= 201402L
// C++14 features
void example() {
auto lambda = [](auto x) { return x + x; };
std::cout << lambda(5) << std::endl; // Outputs: 10
}
#else
// Fallback code for older C++ standards
void example() {
std::cout << "C++14 features not supported." << std::endl;
}
#endif
int main() {
example();
return 0;
}
How do I avoid rehashing overhead with std::set in multithreaded code?
How do I find elements with custom comparators with std::set for embedded targets?
How do I erase elements while iterating with std::set for embedded targets?
How do I provide stable iteration order with std::unordered_map for large datasets?
How do I reserve capacity ahead of time with std::unordered_map for large datasets?
How do I erase elements while iterating with std::unordered_map in multithreaded code?
How do I provide stable iteration order with std::map for embedded targets?
How do I provide stable iteration order with std::map in multithreaded code?
How do I avoid rehashing overhead with std::map in performance-sensitive code?
How do I merge two containers efficiently with std::map for embedded targets?