Concepts in C++20

Using Trailing Requires Clauses

What are trailing requires clauses and when should I use them?

Abstract art representing computer hardware

Trailing requires clauses are a way to specify constraints on function templates or member functions of class templates. They are placed after the function signature and before the function body.

Trailing requires clauses are particularly useful when you want to conditionally enable or disable member functions based on the template parameters of the class. Here's an example:

#include <concepts>
#include <iostream>

template <typename T>
class Container {
  T value{T(3.14)};

 public:
  void printValue()
    requires std::integral<T>
  {
    std::cout << "Integral value: " << value
      << '\n';
  }

  void printValue()
    requires std::floating_point<T>
  {
    std::cout << "Floating-point value: "
      << value << '\n';
  }
};

int main() {
  Container<int> intContainer;
  // Calls the integral version
  intContainer.printValue();

  Container<double> doubleContainer;
  // Calls the floating-point version
  doubleContainer.printValue();
}
Integral value: 3
Floating-point value: 3.14

In this example, the Container class template has two overloads of the printValue member function, each with a different trailing requires clause.

The first overload is enabled only when the template parameter T satisfies the std::integral concept, while the second overload is enabled only when T satisfies the std::floating_point concept.

This allows the appropriate version of printValue to be called based on the type used to instantiate the Container template.

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

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:

  • 124 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