关于功能模板专业化,我想帮助一些语法。以下是简化方案:
基本标题:
template <typename T>
void Foo(T* t) { TRACE("Default Foo impl"); } // <-- default implementation
template <typename T>
struct Base
{
explicit Base()
{
static_assert(std::is_base_of<Base, T>::value, "T must derive from Base");
Foo(static_cast<T*>(this));
}
};
Derived_X标题:
struct Derived_X : public Base<Derived_X>
{
explicit Derived_X() : Base<Derived_X> { } { }
};
// no specialization will be implemented --> using default
Derived_Y标题:
struct Derived_Y : public Base<Derived_Y>
{
explicit Derived_Y() : Base<Derived_Y> { } { }
};
template <> // Foo specialization for Derived_Y
void Foo<Derived_Y>(Derived_Y* t)
{
Foo(static_cast<Base<Derived_Y>*>(t)); // <-- call default impl
TRACE("Derived_Y Foo impl");
}
Derived_Z标题:
template <typename T>
struct Derived_Z : public Base<T>
{
explicit Derived_Z() : Base<T> { }
{
static_assert(std::is_base_of<Derived_Z, T>::value, "T must derive from Derived_Z");
}
};
/* What does this specialization look like?
template <typename T>
void Foo<Derived_Z<T>>(Derived_Z<T>* t)
{
Foo(static_cast<Base<T>*>(t)); // <-- call default impl
TRACE("Derived_Z<T> Foo impl");
}
// */
MostDerived Header:
struct MostDerived : public Derived_Z<MostDerived>
{
explicit MostDerived() : Derived_Z<MostDerived> { } { }
};
template <>
void Foo<MostDerived>(MostDerived* t)
{
Foo(static_cast<Derived_Z<MostDerived>*>(t)); // <-- call Derived_Z impl
TRACE("MostDerived Foo impl");
}
用法:
int main()
{
Derived_X dx { }; // <-- "Default Foo impl"
Derived_Y dy { }; // <-- "Default Foo impl" && "Derived_Y Foo impl"
MostDerived md { }; // <-- "Default Foo impl" && "MostDerived Foo impl"
}
我无法确定如何为Foo
专门化Derived_Z
。任何帮助都将非常感激!
答案 0 :(得分:3)
通常认为专门化功能模板的形式很差。您将无法部分专业化(如您所注意到的),并且不会在重载决策中考虑它们。相反,只需创建重载,并让重载解析来处理其余的事情。
// no template at all for Derived_Y
void Foo(Derived_Y* t)
{
Foo(static_cast<Base<Derived_Y>*>(t)); // <-- call default impl
TRACE("Derived_Y Foo impl");
}
// a regular template (no specialization) for Derived_Z<T>
template <typename T>
void Foo(Derived_Z<T>* t)
{
Foo(static_cast<Base<T>*>(t)); // <-- call default impl
TRACE("Derived_Z<T> Foo impl");
}
// again, no template for MostDerived
void Foo(MostDerived* t)
{
Foo(static_cast<Derived_Z<MostDerived>*>(t)); // <-- call Derived_Z impl
TRACE("MostDerived Foo impl");
}
现在,您可能需要考虑将基本实现更改为仅接受Base<T>*
而不是T*
。假设您Derived_Y2
派生自Derived_Y
,但您没有为Foo(Derived_Y2*)
定义重载。然后使用指向Foo()
的指针调用Derived_Y2
将转到Foo(T*)
,T
被推断为Derived_Y2
,因为这是比{{更好的匹配1}}
Foo(Derived_Y*)
将基本实现更改为:
struct Derived_Y2 : Derived_T { };
Derived_Y2 y2; // "Default Foo impl"
当给出指向template<class T>
void Foo(Base<T>*) { TRACE("Default Foo impl"); }
的指针时, Foo(Derived_Y*)
现在将更好地匹配,因为它们正如他们所说的那样更专业。
答案 1 :(得分:1)
我无法确定如何为Derived_Z专门化Foo。
这是因为你不能部分专门化模板功能。
但您可以部分专门化class
/ struct
。
所以我建议使用帮助器struct
template <typename T>
struct bar
{
static void func (T *)
{ std::cout << "default bar" << std::endl; }
};
template <>
struct bar<Derived_Y>
{
static void func (Derived_Y *)
{ std::cout << "Derived_Y bar" << std::endl; }
};
template <typename T>
struct bar<Derived_Z<T>>
{
static void func (Derived_Z<T> *)
{ std::cout << "Derived_Z<T> bar" << std::endl; }
};
和Foo()
只是变成
template <typename T>
void Foo (T * t)
{ bar<T>::func(t); }