49

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

qft
  • 705
  • 6
  • 9

1 Answers1

40

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.

SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
  • 6
    To clarify this phrase: `it will create a compiler error if the condition is not met`: Is it fair to say this triggers SFINAE ("Substitution Failure Is Not An Error") and the constructor is not defined? (I am a bit new to SFINAE.) – kevinarpe Oct 03 '16 at 09:03