Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does "typename =" mean in the template parameters?

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...

like image 663
qft Avatar asked Mar 19 '15 03:03

qft


People also ask

What is use of typename keyword in C++?

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.

Why is typename needed?

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).

What is the difference between class and typename in template?

There is no difference between using <typename T> OR <class T> ; i.e. it is a convention used by C++ programmers.

What is the difference between template typename T and template T?

There is no difference. typename and class are interchangeable in the declaration of a type template parameter.


1 Answers

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.

like image 179
SLaks Avatar answered Sep 21 '22 14:09

SLaks