3 ms·
structurally you can do something similar by adding some tag (in the form of a constant or nested type) to your class. For example: template<class T> concept
by gpderetta 4y ago
structurally you can do something similar by adding some tag (in the form of a constant or nested type) to your class. For example:
template<class T> concept serializable = requires { typename T::is_serializable_tag; };
void serialize(serializable auto x) {...};
struct NotSerializableClass { ... };
struct SerializableClass { using is_serializable_tag = void; ... };
serialize(NotSerializableClass{}); // error
serialize(SerializableClass{}); // all good
In C++, specializing a trait is also an option. So, while nominal and structural interfaces are not the same, sometimes the lines are blurred.