我正在尝试在命名空间内调用一个重载函数,但有些困难。
示例1: 没有命名空间
为什么会出现这种情况?我需要做什么才能在模板函数之后声明或定义重载函数?
示例1: 没有命名空间
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;
}
示例2:在模板之前定义所有重载
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;
}
错误示例3: 模板后面的一些重载
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;
}
为什么会出现这种情况?我需要做什么才能在模板函数之后声明或定义重载函数?
inline
吗? - crushinline
。 - Dark Falcon