关于函数模板专用化,我需要一些语法方面的帮助。 下面是一个简化的方案:
基本标头:
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");
}
// */
最派生标头:
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"
}
我无法确定如何为Derived_Z
专门Foo
。 任何帮助将不胜感激!
通常认为专用函数模板的形式很差。您将无法部分专业化(正如您所注意到的(,并且它们不会在过载分辨率中被考虑。相反,只需创建重载,让重载解决处理其余的工作。
// 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_Y
的Derived_Y2
,但您尚未定义 Foo(Derived_Y2*)
的重载。使用指向Derived_Y2
的指针调用Foo()
将进入Foo(T*)
,T
被推导为Derived_Y2
,因为这比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"); }
当给定指向Derived_Y2
的指针时,Foo(Derived_Y*)
现在将是一个更好的匹配,因为它正如他们所说更专业。
我无法确定如何为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); }