Creating Custom Concepts

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?

Abstract art representing computer hardware

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.

This Question is from the Lesson:

Creating Custom Concepts

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

Answers to questions are automatically generated and may not have been reviewed.

This Question is from the Lesson:

Creating Custom Concepts

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

A computer programmer
Part of the course:

Professional C++

Comprehensive course covering advanced concepts, and how to use them on large-scale projects.

Free, unlimited access

This course includes:

  • 125 Lessons
  • 550+ Code Samples
  • 96% Positive Reviews
  • Regularly Updated
  • Help and FAQ
Free, Unlimited Access

Professional C++

Comprehensive course covering advanced concepts, and how to use them on large-scale projects.

Screenshot from Warhammer: Total War
Screenshot from Tomb Raider
Screenshot from Jedi: Fallen Order
Contact|Privacy Policy|Terms of Use
Copyright © 2024 - All Rights Reserved