这是很容易的,我坚持要弄清楚如何实际使用,模板专业化需要
template<class C>
struct P{
};
template<>
struct P<int>{};
是否有这样的特定专业化实际的应用?
这是很容易的,我坚持要弄清楚如何实际使用,模板专业化需要
template<class C>
struct P{
};
template<>
struct P<int>{};
是否有这样的特定专业化实际的应用?
虽然您的具体示例看起来不太有趣,但模板专业化被广泛使用。下面是一个例子:
template< class T >
struct StringConverter
{
static std::string Convert(const T&)
{
//do some conversion from T to string using stringstream for instance
//and return result
}
}
template<>
struct StringConverter<std::string>
{
static std::string Convert(const std::string& t)
{
//no need for conversion here!
return t;
}
}
//usage:
std::string a = StringConverter<int>::Convert(5); //default impl
std::string b = StringConverter<std::string>::Convert("b"); //specialized
没有任何上下文,有点难以分辨。 –
就像你想说的那样,他们*实际* *都是空的? –
由于其他原因,这实际上并不是一件好事,它实际上是在STL内部完成的,'std :: vector'是'std :: vector <>'模板的一个特例 –