Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does template argument deduction work when an overloaded function is involved as an argument?

Tags:

This is the more sophisticated question mentioned in How does overload resolution work when an argument is an overloaded function?

Below code compiles without any problem:

void foo() {} void foo(int) {} void foo(double) {} void foo(int, double) {}  // Uncommenting below line break compilation //template<class T> void foo(T) {}  template<class X, class Y> void bar(void (*f)(X, Y)) {     f(X(), Y()); }  int main() {     bar(foo); } 

It doesn't appear a challenging task for template argument deduction - there is only one function foo() that accepts two arguments. However, uncommenting the template overload of foo() (which still has just a single parameter) breaks compilation for no obvious reason. Compilation fails both with gcc 5.x/6.x and clang 3.9.

Can it be explained by the rules of overload resolution/template argument deduction or it should be qualified as a defect in those compilers?

like image 708
Leon Avatar asked Nov 01 '16 09:11

Leon


People also ask

What is template argument deduction?

Template argument deduction is used in declarations of functions, when deducing the meaning of the auto specifier in the function's return type, from the return statement.

What is a template argument?

A template parameter is a special kind of parameter that can be used to pass a type as argument: just like regular function parameters can be used to pass values to a function, template parameters allow to pass also types to a function.

How we can overload the function template in C++?

You may overload a function template either by a non-template function or by another function template. The function call f(1, 2) could match the argument types of both the template function and the non-template function.

What is a deduction guide?

Template deduction guides are patterns associated with a template class that tell the compiler how to translate a set of constructor arguments (and their types) into template parameters for the class. The simplest example is that of std::vector and its constructor that takes an iterator pair.


1 Answers

As noted in the answer to your linked question:

[temp.deduct.call]/6:When P is a function type, pointer to function type, or pointer to member function type:

— If the argument is an overload set containing one or more function templates, the parameter is treated as a non-deduced context.

Since the overload set contains a function template, the parameter is treated as a non-deduced context. This causes template argument deduction to fail:

[temp.deduct.type]/4: [...]If a template parameter is used only in non-deduced contexts and is not explicitly specified, template argument deduction fails.

And this failed deduction gives you your error. Note that if you explicitly specify the arguments, the code compiles successfully:

bar<int,double>(foo); 

Live demo

like image 102
TartanLlama Avatar answered Oct 21 '22 21:10

TartanLlama