Why additional argument allows calling from a template some function declared later?

72 views Asked by At

I would like to write a template that calls some function declared later in the code. The simplest approach does not work, unfortunately:

template<class T>
void g(T t) { f(t); }

template<class T>
void f(T) {}

because of the error:

call to function 'f' that is neither visible in the template definition nor found by argument-dependent lookup

Online demo: https://godbolt.org/z/8Mh9rYzvW

But if I add an additional unused argument to the calling function that does not depend on the template parameter:

struct A{};

template<class T>
void g(T t) { f(A{}, t); }

template<class T>
void f(A, T) {}

it starts working magically, online demo: https://godbolt.org/z/YqqWvWosM

Could you please explain how this additional argument helps here? Is it because of argument-dependent lookup that is performed additionally for A?

0

There are 0 answers