【发布时间】:2012-07-12 14:12:00
【问题描述】:
假设我有这样的功能:
template<typename T> inline
typename std::enable_if<has_member_foo<T>::value,int>::type
foo( T const &t ) {
return t.foo();
}
template<typename T> inline
typename std::enable_if<!has_member_foo<T>::value,int>::type
foo( T const& ) {
return 0;
}
template<typename T> inline
int call_foo( T const &t ) {
return sizeof( T ) + foo( t );
}
这大部分都可以正常工作,但如果我稍后为特定类型添加重载:
inline int foo( std::string const &s ) {
return s.size();
}
我在call_foo() 的定义之后添加它,
call_foo() 不使用重载。
但是,如果我将重载代码移动到call_foo() 的定义之前,则会使用它。
为什么在第一种情况下不使用重载?
当call_foo() 在代码的其他地方被实例化时,
编译器已经看到重载,
那它为什么不使用它呢?
请注意,我的原始代码将foo() 函数作为模板化foo_traits 类的静态成员函数,同样使用enable_if 进行保护。 代码可以工作,即模板类特化,即使在使用call_foo() 之后提供,那么为什么不用于独立的重载函数呢?
如果重要的话,我在 Mac OS X 10.7.4 上使用 g++ 4.6。
【问题讨论】:
-
吹毛求疵:您的
foo(std::string const&)是重载,而不是特化(它不是模板函数)。
标签: c++ templates template-specialization