Open In App

C++ 20 – Feature Test Macros

Last Updated : 22 Sep, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

In C++, the stage is set for the entry of Feature Test Macros, a potent mechanism ushered in by C++20 to confront these compatibility quandaries head-on. Within this article, we shall embark on an exploration of Feature Test Macros, delve into their conceptual essence, grasp their significance, navigate through tangible examples, and discern their role in the art of composing portable, harmonious C++ code.

Features Test Macros in C++

Feature Test Macros emerge as preprocessor directives that grant you the ability to scrutinize whether a particular language or library feature garners support from the compiler during the compilation process. This avenue furnishes a uniform method for querying the capabilities of the compiler, facilitating a seamless adjustment of your codebase in response.

Through the strategic employment of Feature Test Macros, developers attain the capacity to discern the availability of select features. As a result, this empowers the conditional assembly of code segments contingent upon the existence or absence of specific attributes. The overarching result is the preservation of code functionality across an array of compilers and diverse renditions of the C++ standard.

Example of Exploring Feature Test Macros

C++




// C++ Program to demonstrate
// Exploring Feature Test Macros 
#include <iostream>
using namespace std;
  
// main function
int main()
{
    // Check if ranges library is available
    #ifdef __cpp_lib_ranges
        cout << "Ranges library is Available." << endl;
    #else
        cout << "Ranges library is Not Available." << endl;
    #endif
  
    return 0;
}


Output

Ranges library is Not Available.



In this example, we use Feature Test Macros to check for the availability of three features: constexpr, the ranges library and structured bindings. Comments are provided alongside each check to explain the purpose of the code block.

Implementing Feature Test Macros

Executing Feature Test Macros efficiently involves the following systematic approach:

  1. Identify the Feature: Commence by pinpointing the particular language or library feature necessitating verification. Consult the official C++ documentation for an array of pre-established macros tailored to C++20 features.
  2. Utilize Predefined Macros: C++20 introduces a suite of predefined macros prefixed with _cpp. These macros are followed by the feature’s name in snake_case. Harness these macros as tools to assess the presence of the desired feature.
  3. Enable Conditional Compilation: Encapsulate code segments contingent on specific features within #ifdef and #endif preprocessor directives. When the macro is defined, the corresponding code block is integrated during the compilation process; conversely, if the macro remains undefined, the block is omitted from compilation.

Exploring Feature Test Macros with Concrete Examples

1. Verifying ‘constexpr’ Support

The constexpr keyword offers compile-time evaluation of expressions, optimizing performance and enabling the direct definition of constants within code.

To assess if the compiler endorses constexpr consider the following snippet:

C++




// C++ Program to demonstrate
// Verifying 'constexpr' Support
#include <iostream>
using namespace std;
  
// main function
int main() {  
    #ifdef __cpp_constexpr
        cout << "Support" << endl;
    #else
        cout << "Not Support" << endl;
    #endif
  
    return 0;
}


Output

Support

2. Gauging Availability of the Ranges Library

Introduced in C++20, the ranges library furnishes a succinct way to manipulate sequences of data. However, not all compilers may have integrated this feature.

To scrutinize the presence of the ranges library, examine this example:

C++




// C++ Program to demonstrate
// Gauging Availability of the 
// Ranges Library
#include <iostream>
using namespace std;
  
// main function
int main() {
    #ifdef __cpp_lib_ranges
        cout << "Available" << endl;
    #else
        cout << "Not Available" << endl;
    #endif
  
    return 0;
}


Output

Available

3. Detecting Support for Structured Bindings

Structured bindings streamline the unpacking of tuple-like or struct-like objects enhancing code readability. To discern whether structured bindings are supported consider this code snippet:

C++




// C++ Program to demonstrate
// Detecting Support for 
// Structured Bindings
#include <iostream>
using namespace std;
  
// main function
int main() {
    #ifdef __cpp_structured_bindings
        cout << "Support" << endl;
    #else
        cout << "Not Support" << endl;
    #endif
  
    return 0;
}


Output

Support

4. Exploring Additional Features

Extend this exploration to encompass other C++20 features of interest. For instance, you might wish to examine the availability of consteval a feature designating functions for compile-time evaluation:

C++




// C++ Program to demonstrate
// Exploring Additional Features
#include <iostream>
using namespace std;
  
// main function
int main()
{
    #ifdef __cpp_consteval
        cout << "Support" << endl;
    #else
        cout << "Not Support" << endl;
    #endif
  
    return 0;
}


Output

Support

Advantages of Utilizing Feature Test Macros

The advantages of Utilizing Feature Test Macros are mentioned below:

  1. Enhanced Compatibility: Given the diverse pace at which distinct compilers incorporate novel features, Feature Test Macros emerge as a potent tool. By leveraging these macros, you can construct code that seamlessly adapts to the compiler’s capabilities. This consistency ensures users encounter a uniform experience across varying environments.
  2. Streamlined Code Maintenance: In contrast to relying on compiler-specific checks or version-based comparisons, Feature Test Macros offer a standardized avenue for identifying features. This streamlined approach significantly simplifies the maintenance of code, curbing the necessity for a multitude of conditional checks.
  3. Embracing Forward Compatibility: As fresh iterations of the C++ standard come into play, compilers often introduce feature flags to facilitate transitions. Feature Test Macros empower you to craft code that stands as a testament to forward compatibility. This foresight paves the way for the effortless assimilation of emerging language features when they eventually come to fruition.


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads