Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Template argument deduction for an argument of a function type

Consider the following program.

#include <iostream>

template <typename T>
void f( void ( *fn )( T ) )
{
    fn( 42 );
}

void g( int x )
{
    std::cout << "g( " << x << " );\n";
}

int main()
{
    f( g );
}

The program compiles successfully and its output is

g( 42 );

Now let's rename the non-template function g to f.

#include <iostream>

template <typename T>
void f( void ( *fn )( T ) )
{
    fn( 42 );
}

void f( int x )
{
    std::cout << "f( " << x << " );\n"; 
}

int main()
{
    f( f );
}

Now the program is not compiled by gcc HEAD 10.0.0 20200 and clang HEAD 10.0.0 but compiled successfully by Visual C++ 2019..

For example the compiler gcc issues the following set of messages.

prog.cc: In function 'int main()':
prog.cc:22:10: error: no matching function for call to 'f(<unresolved overloaded function type>)'
   22 |     f( f );
      |          ^
prog.cc:4:6: note: candidate: 'template<class T> void f(void (*)(T))'
    4 | void f( void ( *fn )( T ) )
      |      ^
prog.cc:4:6: note:   template argument deduction/substitution failed:
prog.cc:22:10: note:   couldn't deduce template parameter 'T'
   22 |     f( f );
      |          ^
prog.cc:14:6: note: candidate: 'void f(int)'
   14 | void f( int x )
      |      ^
prog.cc:14:13: note:   no known conversion for argument 1 from '<unresolved overloaded function type>' to 'int'
   14 | void f( int x )
      |         ~~~~^

So a question arises: should the code be compiled and what is the reason that the code is not compiled by gcc and clang?

like image 526
Vlad from Moscow Avatar asked Jan 17 '20 12:01

Vlad from Moscow


People also ask

What are template arguments enlist types of template arguments?

A template argument for a template template parameter is the name of a class template. When the compiler tries to find a template to match the template template argument, it only considers primary class templates. (A primary template is the template that is being specialized.)

Can there be more than one argument to template?

As seen in the above cited example, we can use more than one parameter for a template in C++.

How do I restrict a template type in C++?

There are ways to restrict the types you can use inside a template you write by using specific typedefs inside your template. This will ensure that the compilation of the template specialisation for a type that does not include that particular typedef will fail, so you can selectively support/not support certain types.

Which of the following can be passed as type arguments to templates?

Explanation: A template parameter is a special kind of parameter that can be used to pass a type as argument. 2.


1 Answers

It would seem to me that gcc and clang are correct. This should not compile. The function parameter from which you'd like T to be deduced becomes a non-deduced context here the moment the argument supplied is an overload set that contains a function template [temp.deduct.type]/5.5:

The non-deduced contexts are:

  • […]
  • A function parameter for which argument deduction cannot be done because the associated function argument is a function, or a set of overloaded functions ([over.over]), and one or more of the following apply:

    • […]
    • the set of functions supplied as an argument contains one or more function templates.
  • […]

Thus, T cannot be deduced and the other overload is not viable due to there being no conversion; exactly what gcc says…

like image 126
Michael Kenzel Avatar answered Oct 04 '22 02:10

Michael Kenzel