有一个大脑放屁......是否有可能做出像这样的工作?
template<int a> struct Foo
{
template<int b> struct Bar;
};
template<int a> struct Foo<a>::Bar<1> //Trying to specialize Bar
{
};
Run Code Online (Sandbox Code Playgroud)
我不具备这样做的,但它可以让我很好地隐藏命名空间范围的一些实施细节.
建议赞赏!
PS:我忘了提到语言不支持明确专门针对Foo范围内的Bar.无论如何,AFAICS.
小智 12
是的你可以.但是你需要改变调用结构,但只需要一点点.
具体来说,使用策略模式将成员函数的实现重组为一个类(允许IS专用).
只要策略类没有嵌套(因此不依赖于非专用模板类型),就允许这样做.
例如(这可能在语法上不正确,但想法应该清楚)
template <class T>
class OuterThingThatIsNotSpecialized
{
template <class U>
void memberWeWantToSpecialize(const U& someObj_)
{
SpecializedStrategy<U>::doStuff(someObj_);
}
};
template <class U>
struct SpecializedStrategy;
template <>
SpecializedStrategy<int>
{
void doStuff(const int&)
{
// int impl
}
};
template <>
SpecializedStrategy<SomeOtherType>
{
void doStuff(const SomeOtherType&)
{
// SOT impl
}
};
Run Code Online (Sandbox Code Playgroud)
这非常有用,因为对没有实现的类型调用OuterThingThatIsNotSpecialized将无法编译.
PS.您甚至可以使用此策略来部分地专门化模板函数,这是C++不可能实现的.
Hos*_*ein 11
Clause 18, Section 14.7.3 of the 1998 standard (ISO14882:1998) says explicit specialisation of (the inner) template member classes is not allowed when the (outer) template class is not explicitly specialised.
根据这些帖子:
http://www.cpptalk.net/template-member-function-specialization-vt11666.html
如果不专门化外部类,则无法专门化模板类的模板成员。他们不引用经文和章节。我的《C++ 编程语言》副本不会立即透露任何内容,而且我的标准副本也在家里(我的备份副本(此处更广为人知的名称为“Chris”)也不在身边:-)