Search code examples
c++templateslanguage-lawyerc++17template-argument-deduction

Do we still need to write the empty angle brackets when using transparent std function objects?


With class template argument deduction we can write:

std::less Fn;

However, G++ 8.2 rejects this code:

#include <algorithm>
#include <vector>
#include <functional>

int main()
{
std::vector v= { 1, 3, 2, 7, 5, 4 };

std::sort(v.begin(),v.end(),std::greater());
}

emitting the following error:

error: cannot deduce template arguments for 'greater' from ()

Clang++ 7.0 and MSVC 15.8.0 compile it without warnings. Which compiler is right?


Solution

  • GCC is wrong. There is already a bug report.

    [dcl.type.simple]/2 says:

    A type-specifier of the form typenameopt nested-name-specifieropt template-name is a placeholder for a deduced class type ([dcl.type.class.deduct]).

    And [dcl.type.class.deduct]/2 says:

    A placeholder for a deduced class type can also be used in the type-specifier-seq in the new-type-id or type-id of a new-expression, as the simple-type-specifier in an explicit type conversion (functional notation) ([expr.type.conv]), or as the type-specifier in the parameter-declaration of a template-parameter. A placeholder for a deduced class type shall not appear in any other context.

    Such use is allowed.


    [temp.arg]/4 describes the syntax error that a template-id is required but there is no <>. However here std::greater is not resolved as a template-id so that paragraph does not apply.