Using requires clause in function templates

How can I use a requires clause in a function template to constrain its arguments based on a concept?

In C++20, you can use a requires clause in a function template to constrain its arguments based on a concept. The requires clause allows you to specify the requirements that the template arguments must satisfy in order for the function template to be considered a viable overload.

Here's an example that demonstrates using a requires clause in a function template:

#include <concepts>
#include <iostream>

template <typename T>
concept Printable = requires(T a) {
  std::cout << a;
};

template <typename T>
requires Printable<T>
void print(T value) {
  std::cout << "Value: " << value << '\n';
}

int main() {
  print(42);
  print("Hello");
  print(3.14);
}

In this example, the Printable concept is defined to check if a type T can be printed to std::cout. The requires expression inside the concept ensures that the expression std::cout << a is valid for an instance a of type T.

The print function template uses a requires clause to constrain its argument value based on the Printable concept. The requires Printable<T> clause specifies that the template argument T must satisfy the Printable concept in order for the function template to be considered a viable overload.

In the main function, we call the print function with different types of arguments. The compiler will check if each argument type satisfies the Printable concept before instantiating the function template.

The output of this program will be:

Value: 42
Value: Hello
Value: 3.14

By using a requires clause in a function template, you can enforce specific requirements on the template arguments based on concepts. This allows you to write more expressive and type-safe code, ensuring that the function is only called with arguments that meet the specified requirements.

Note that the requires clause can be used in various places within a template declaration, such as function parameters, return types, and template parameters. It provides a powerful way to constrain and specialize templates based on concepts.

Creating Custom Concepts

Learn how to create your own C++20 concepts to define precise requirements for types, using boolean expressions and requires statements.

Questions & Answers

Answers are generated by AI models and may not have been reviewed. Be mindful when running any code on your device.

Difference between requires keyword and requires expression
What is the difference between the requires keyword and a requires expression in C++20 concepts?
Negating requires expressions in C++20 concepts
How can I negate a requires expression in a C++20 concept to specify that a type should not satisfy certain requirements?
Using multiple type parameters in C++20 concepts
Can I define a concept that takes multiple type parameters in C++20? How can I use such a concept to constrain a template?
Using concepts to constrain auto parameters
Can I use concepts to constrain auto parameters in functions? How does it work?
Or Ask your Own Question
Get an immediate answer to your specific question using our AI assistant