Using Concepts with Classes

Constraining Class Templates

Can I use concepts to constrain class templates?

Abstract art representing computer hardware

Yes, you can use concepts to constrain class templates, similar to how you constrain function templates. Here's an example:

#include <concepts>
#include <iostream>

template <typename T>
concept Printable = requires(T obj) {
  { obj.Print() } -> std::same_as<void>;
};

template <Printable T>
class Logger {
 public:
  void Log(const T& obj) { obj.Print(); }
};

struct MyClass {
  void Print() const {
    std::cout << "Hello World";
  }
};

int main() {
  Logger<MyClass> logger;
  MyClass obj;
  logger.Log(obj);
}
Hello World

In this example, the Printable concept requires a type T to have a Print() member function that returns void.

The Logger class template is constrained using the Printable concept, specifying that the template parameter T must satisfy the concept's requirements.

Inside the Logger class, the Log member function accepts an object of type T and calls its Print() function.

The MyClass struct satisfies the Printable concept by providing the required Print() member function.

In the main function, we can create an instance of Logger<MyClass> because MyClass meets the Printable concept's requirements. We can then use the Log member function to log an object of MyClass.

By constraining class templates with concepts, we ensure that the class can only be instantiated with types that fulfill the specified requirements. This helps catch errors at compile-time and provides better documentation and clarity about the expectations of the class template.

This Question is from the Lesson:

Using Concepts with Classes

Learn how to use concepts to express constraints on classes, ensuring they have particular members, methods, and operators.

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

This Question is from the Lesson:

Using Concepts with Classes

Learn how to use concepts to express constraints on classes, ensuring they have particular members, methods, and operators.

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