Constraining Class Templates

Can I use concepts to constrain class templates?

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.

Using Concepts with Classes

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

Questions & Answers

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

Constraining Member Variable Type
How can I require a class member variable to be of a specific type using concepts?
Requiring Method Signature
Can I constrain a class to have a method with a specific signature using concepts?
Requiring Equality Operator
How can I ensure a class has a proper equality operator using concepts?
Checking Member Type with Type Traits
Can I use type traits to check the type of a class member within a concept?
SFINAE vs Concepts
How do C++ concepts compare to SFINAE for constraining templates?
Requiring Multiple Member Functions
How can I require a class to have multiple member functions using concepts?
Or Ask your Own Question
Get an immediate answer to your specific question using our AI assistant