问题描述
template<typename T>
class CConstraint
{
public:
CConstraint()
{
}
virtual ~CConstraint()
{
}
template <typename TL>
void Verify(int position, int constraints[])
{
}
template <>
void Verify<int>(int, int[])
{
}
};
在g ++下编译此语句会产生以下错误:
Compiling this under g++ gives the following error:
非命名空间作用域中的显式专门化'class CConstraint'
Explicit specialization in non-namespace scope 'class CConstraint'
在VC中,它编译得很好。任何人都可以让我知道解决方法?
In VC, it compiles fine. Can anyone please let me know the workaround?
推荐答案
VC ++在这种情况下不符合 - 显式专门化必须在命名空间范围。 C ++ 03,§14.7.3/ 2 :
VC++ is non-compliant in this case - explicit specializations have to be at namespace scope. C++03, §14.7.3/2:
此外,由于C ++ 03,§14.7.3/ 3 ,不能专门化成员函数而不明确专门化包含类,因此一个解决方案是让 Verify / code>转发到一个可能是专门的免费函数:
Additionally you have the problem that you can't specialize member functions without explicitly specializing the containing class due to C++03, §14.7.3/3, so one solution would be to let Verify()
forward to a, possibly specialized, free function:
namespace detail {
template <typename TL> void Verify (int, int[]) {}
template <> void Verify<int>(int, int[]) {}
}
template<typename T> class CConstraint {
// ...
template <typename TL> void Verify(int position, int constraints[]) {
detail::Verify<TL>(position, constraints);
}
};
这篇关于非命名空间范围中的显式专门化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!