I've recently encountered an interesting version of enable_if usage to conditionally enable a function with slightly better readability because the function's return type is not part of enable_if (see cleaner_usage):
#include <type_traits>
using return_type = int;
std::enable_if<std::is_integral<float>::value, return_type>::type
traditional_usage()
{
return 1;
}
template<typename std::enable_if<std::is_integral<float>::value, int>::type = 0>
return_type cleaner_usage()
{
return 2;
}
int main()
{
return traditional_usage() + cleaner_usage();
}
For failure-case the mechanism is clear (no type member).
But how exactly does it work in other cases? Because it looks like int typedef field substituted into template type parameter with unexpected assignment.
Aucun commentaire:
Enregistrer un commentaire