【发布时间】:2014-02-02 10:19:23
【问题描述】:
以下代码在 gcc 4.5.3 上编译失败
struct Frobnigator
{
template<typename T>
void foo();
template<typename T>
void bar();
};
template<typename T>
void Frobnigator::bar()
{
}
template<typename T>
void Frobnigator::foo()
{
bar<T>();
}
template<> // error
void Frobnigator::foo<bool>()
{
bar<bool>();
}
template<>
void Frobnigator::bar<bool>()
{
}
int main()
{
}
错误消息:specialization of ‘void Frobnigator::bar() [with T = bool]’ after instantiation。我终于通过将Frobnigator::bar<bool>() 的特化出现在Frobnigator::foo<bool>() 之前解决了这个问题。显然,方法出现的顺序很重要。
那么为什么上面代码的以下精简版,其中bar的特化出现在通用版本之后,有效?
struct Frobnigator
{
template<typename T>
void foo();
};
template<typename T>
void Frobnigator::bar()
{
}
template<>
void Frobnigator::bar<bool>()
{
}
int main()
{
}
【问题讨论】:
标签: c++ templates template-specialization