Feature-test macros in C++ are used to conditionally compile sections of code depending on whether a specific feature is available in the compiler or not. This allows you to write code that is portable across different compilers and versions of C++. Here's how you can use them effectively:
To check for specific features, you typically use `#if`, `#ifdef`, or `#ifndef` directives along with predefined macros like `__cplusplus` or compiler-specific macros. Below is a practical example:
#include
// Check if C++17 is supported
#if __cplusplus >= 201703L
#define FEATURE_SUPPORTED true
#else
#define FEATURE_SUPPORTED false
#endif
int main() {
if (FEATURE_SUPPORTED) {
std::cout << "C++17 features are supported!" << std::endl;
// Code that uses C++17 features
} else {
std::cout << "C++17 features are not supported." << std::endl;
// Code for older C++ standard
}
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?