Creating Custom Concepts

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?

Abstract art representing computer hardware

Yes, you can define concepts that take multiple type parameters in C++20. This allows you to express relationships and constraints between different types. When using such a concept to constrain a template, you need to provide the additional type arguments explicitly.

Here's an example that demonstrates a concept with multiple type parameters:

#include <concepts>
#include <iostream>
#include <string>

template <typename T, typename U>
concept Convertible = std::is_convertible_v<T, U>;

template <typename T, typename U>
  requires Convertible<T, U>
U convert(T value) {
  return static_cast<U>(value);
}

int main() {
  double result1 = convert<int, double>(42);
  std::cout << "Converted value: "
    << result1 << '\n';

  std::string result2 = convert<
    const char*, std::string>("Hello");
  std::cout << "Converted value: "
    << result2 << '\n';
}

In this example, the Convertible concept takes two type parameters, T and U. It uses the std::is_convertible_v type trait to check if T is implicitly convertible to U.

The convert function template is constrained by the Convertible concept using a requires clause. The requires Convertible<T, U> clause specifies that T and U must satisfy the Convertible concept, meaning that T must be convertible to U. The function takes a parameter of type T and returns a value of type U.

When calling the convert function, you need to provide both the T and U template arguments explicitly, as shown in the main function.

The output of this program will be:

Converted value: 42
Converted value: Hello

By using multiple type parameters in concepts, you can express more complex relationships and constraints between types. This allows you to write more expressive and reusable templates that enforce specific type requirements.

Remember that when using a concept with multiple type parameters to constrain a template, you need to provide the additional type arguments explicitly when instantiating the template. The requires clause is used to specify the concept constraint on the template parameters.

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:

  • 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