I am attempting to call an overloaded function inside a namespace and am struggling a bit.
Working Example 1: No namespace
class C {};
inline void overloaded(int) {}
template<typename T> void try_it(T value) {
overloaded(value);
}
inline void overloaded(C) {}
int main()
{
try_it(1);
C c;
try_it(c);
return 0;
}
Working Example 2: All overloads defined before template
class C {};
namespace n {
inline void overloaded(int) {}
inline void overloaded(C) {}
}
template<typename T> void try_it(T value) {
n::overloaded(value);
}
int main()
{
try_it(1);
C c;
try_it(c);
return 0;
}
Broken Example 3: Some overloads after the template
class C {};
namespace n {
inline void overloaded(int) {}
}
template<typename T> void try_it(T value) {
n::overloaded(value);
}
namespace n {
inline void overloaded(C) {}
}
int main()
{
try_it(1);
C c;
try_it(c); // /tmp/test.cpp: In function ‘void try_it(T) [with T = C]’:
// /tmp/test.cpp:19:15: instantiated from here
// /tmp/test.cpp:8:7: error: cannot convert ‘C’ to ‘int’ for argument ‘1’ to ‘void n::overloaded(int)’
return 0;
}
Why is this the case? What do I need to do to be able to declare or define an overload after the template function?