Using trailing return type (1), you can actually have a foretaste of concepts:
template <typename Type>
auto f(std::ostream& out, Type const& t) -> decltype(out << t, void()) {
// ...
}
Because of SFINAE, this overload can only be selected if the type of out << t
can be resolved, and this implies that an overload of <<
exists that accepts both parameters.
The one pitfall is that this does not work if you need the contrary, that is enabling a function if this overload does not exists. In this case an enable_if
strategy (and the symmetric disable_if
) is necessary, as far as I know.
(1) thanks to Simple for helping out with the syntax