I found this construct in C-code:
template<typename T, class = decltype(std::declval<T>() < std::declval<T>())>
struct check : std::true_type {};
Now I understand what it does but I don't understand how it works. It throws a compile error if type T
doesn't support the <
-operator. But, apparently, when changing class
to something else, the whole thing won't compile and throws a Syntax Error.
What does class = sometypename
mean?
class
is the same as typename
here. You could also do this:
template<typename T, typename = decltype(std::declval<T>() < std::declval<T>())>
struct check : std::true_type {};
You can specify default values for template arguments. For example
template<typename X = int> struct test { };
You can also leave off the name of the template arguments if you don't use them:
template<typename = int> struct test { };
So in your example, the second template parameter is just an unnamed parameter with a default argument.
使这个工作称为SFINAE概念(取代失败不是错误)和用于实现std::enable_if<>
等http://en.cppreference.com/w/cpp/language/sfinae
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.