问题
I have seen this expression in page 189 of the book "Effective Modern C++":
template<typename T,
typename = typename std::enable_if<condition>::type>
explicit Person(T&& n);
I am just wondering what does the part "typename =
" mean. It certainly looks like a default argument for a template parameter. But don't you need something like "typename some_name = ...
" in a default argument? There is no name for the second template argument, and I don't see the second template argument being used in this case.
P.S. When I search on google (or any other search engine) for an answer, the equal sign is always discarded, and this just makes finding an answer almost impossible...
回答1:
That's an optional template parameter with no name and a default value.
It's used to apply the enable_if
condition; it will create a compiler error if the condition is not met.
You can use exactly the same syntax for normal method arguments.
来源:https://stackoverflow.com/questions/29136749/what-does-typename-mean-in-the-template-parameters