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 WorldIn 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.