- Provided a way to specify constraints on template parameters, improving code readability and compile-time error messages.
- Introduced a new way to work with sequences, enabling more expressive and efficient code.
- Allowed for asynchronous programming and non-blocking code execution, enhancing performance in concurrent applications.
Learn about concept in C++ 20
The most recent version of C++ was released in 2020 as C++ 20. It includes several enhancements and new features that increases the flexibility and capability of the language. This blog will examine some of the most essential and valuable features of C++20 with example codes that might be helpful for programmers.
In this blog, we'll be discussing concept with various examples.
concept#
concept is a new language feature introduced in C++20. It is defined in the <concepts> header. It provides a way to define requirements on template arguments, enabling more concise and readable code. This is achieved by allowing the programmer to specify constraints on the template parameters. These constraints are checked at compile-time without sacrificing type safety. This helps identify errors in templatized code early in the development cycle, improving code reliability.
Generally, type traits are used to ensure template arguments meet certain requirements. However, type traits can be challenging to understand and can lead to verbose code. Concepts provide a way to define these requirements directly in the template parameter list.
Defining concept#
A concept can be easily defined as follows:
The code above defines an Integral concept, which ensures that the T parameter type is of integral type. This concept can be used to add two numbers in the following way:
The Add template function uses the Integral concept to ensure that the parameters passed are of integral type. The function then adds the two numbers and returns their sum.
In lines 14–15 in the code above, the sum of two int and two char have been performed using the Add function. The code will successfully generate the correct output, as shown below. Remember that ASCII characters are 8-bit, and there will be an overflow.
However, if line 17 is uncommented, the code will generate a compile-time error, as shown below. The Add function is defined for integral values but not for floating point values.
Including a list of constraints#
concept can also be used to include a list of constraints. This can be done using requires. The following code defines a String concept that requires a T type to have a c_str member function that returns a const char *.
The print_string template function is defined using the String concept. It's important to note the use of requires in the definition of the function. This ensures that the T will follow the String concept. The print_string function prints the value using the c_str() function.
User-defined data types#
Let's take another example that explains how a concept with the + operator works with a specific type. In this code, requires is being used to include a list of constraints.
In the example above, the Addable concept requires two objects of type T and is labeled as requires. The expression {x + y} has to be a valid statement, and the result of the statement is convertible to the same type T as x and y.
With this Addable concept, we can write a function that requires its arguments to be addable.
In the example above, the Sum function takes two arguments of type T and requires that T satisfies the Addable concept. This means that the + operator is guaranteed to be available for the arguments, and the function can be used with any type that satisfies the concept.
The following code uses the concept and function defined above to evaluate the sum of two Rational numbers.
In lines 1–10, a user-defined type Rational representing a rational number has been defined. The + operator in lines 6–9 that satisfies the requirements of the Addable concept has been implemented in the structure. Finally, a std::common_type specialization has been provided to specify that the common type of two Rational objects is also Rational. In the main function, two objects of type Rational have been created and initialized. The Sum function has been called using these two objects. The function calls the overloaded + operator and returns the result of the addition, which is stored in another result object and displayed on the console. Moreover, two examples of primitive datatypes, int and double, have also been provided to elaborate on the use of concept. As the concept of the + operator has been defined in this example, the concept can be used to ensure the availability of other operators for different data types.
Refinement#
Let's take another example where concept can be used for refinement. In the following code, a Range has been defined that requires the type T to implement begin and end functions that return an iterator of the same type.
The print_range function in lines 9–13 implements the Range concept to print all container content starting from the first to the last element.
In line 3, a vector has been defined and initialized. In line 4, the print_range function has been called to print the vector. The output is as desired. However, when lines 6–7 are uncommented, an error is generated at compile-time, indicating that no matching function exists for the int array because it doesn't have begin and end functions. This saves any runtime error that may have caused the termination of an execution.
Conclusion#
In short, concept provides a more readable and maintainable way to define constraints on template parameters. It makes it easier to express the requirements of a template argument and to write generic code that works with a wide range of types.
We hope that this blog has not only triggered your quest to learn more about C++ 20 but also inspired you to learn to code in C++ with greater depth. For further readings please continue with the following courses:
C++ Concepts: Improve Type Safety with C++ 20 is all about using concept for type safety.
The All-in-One Guide to C++20 introduces C++20 in great detail.
C++ Fundamentals for Professionals is for refreshing C++.
Frequently Asked Questions
What are the features released in C++20?
What are the features released in C++20?