In the following code
template <typename T>
void foo(T) {
bar(T{});
}
class Something {};
void bar(Something) {}
int main() {
foo(Something{});
}
(https://wandbox.org/permlink/l2hxdZofLjZUoH4q)
When we call foo()
with a Something
parameter, everything works as expected, the call dispatches to the bar(Something)
overload.
But when I change the argument to an integer and provide a bar(int)
overload, I get an error
template <typename T>
void foo(T) {
bar(T{});
}
void bar(int) {}
int main() {
foo(int{});
}
Error:
error: call to function 'bar' that is neither visible in the template definition nor found by argument-dependent lookup
(https://wandbox.org/permlink/GI6wGlJYxGO4svEI)
In the class case, I have not defined bar()
in a namespace along with the definition of Something
. Meaning that I am not getting ADL. Then why does the code work with class types?