How do I use feature-test macros for C++11?

Feature-test macros are a powerful tool in C++ that allows you to check for the availability of specific features or standards in the language. In C++11, feature-test macros can help you write code that gracefully degrades if certain features are not available, ensuring greater portability across different compilers and systems.

To use feature-test macros in C++11, you would typically check for a macro that indicates the availability of a certain feature. For example, if you want to check if C++11 is supported, you can use the macro defined by the compiler. Here’s an example:

#if __cplusplus >= 201103L // C++11 features are available #include int main() { std::cout << "C++11 features are supported!" << std::endl; return 0; } #else // Fallback code for pre-C++11 #include int main() { std::cout << "C++11 features are not supported." << std::endl; return 0; } #endif

C++11 feature-test macros C++ portability C++ features compiler compatibility