I’m trying to wrap my head around concepts
in C++20. Consider the following (very simple) concept:
template <typename T>
concept Float = requires
{
std::floating_point<T>;
};
I can write a templated class using this concept in 2 ways:
template <Float T>
class MyClass {
public:
MyClass(T val) : value{ val } {};
private:
T value;
};
Or I can do
template <typename T> requires Float<T>
class MyClass {
public:
MyClass(T val) : value{ val } {};
private:
T value;
};
In this particular example, these behave exactly the same. Is this always the case though? Are there situations where it is easier to express some constraint using the template <...> requires ...
syntax? Or is this merely a style preference/convention?
The only thing I can think of right now is that using the template <...> requires ...
syntax may allow you represent relational constraints between multiple template arguments, where as the template <CONCEPT T>
syntax only allows you to represent a constraint on a single argument?
Any guidance would be much appreciated.