In C# I am used to specify multiple constraints in a generic method or type as shown right here:
How do I specify multiple generic type constraints on a single method?
Today I started with C++ for the first time and I am unable to find anything useful when googling for template multiple constraints.
It almost looks like this is not possible and everyone knows it, and thus no questions asked.
I know that in C# generics are much stronger validated by the compiler than in C++, and that is why it feels wrong to me not to be able to constrain my types to multiples super-types.
you usually make constraints on C++ template with
std::enable_if
here is the trick - if your template looks like this:
you take the return type and corporate it with
enable_if
like this :here,
XXX
represents your compile-time condition.for example: let
add(T1 t1 , T2 t2)
be compiled for only objects that inherit fromAddable
: