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...
Use the keyword typename if you have a qualified name that refers to a type and depends on a template parameter. Only use the keyword typename in template declarations and definitions.
The typename keyword is needed whenever a type name depends on a template parameter, (so the compiler can 'know' the semantics of an identifier (type or value) without having a full symbol table at the first pass).
There is no difference between using <typename T> OR <class T> ; i.e. it is a convention used by C++ programmers.
There is no difference. typename and class are interchangeable in the declaration of a type template parameter.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With