我希望我的模板函数只接受从基类继承的类作为参数。我认为代码片段可以更好地解释它。
class Base
{
// Some magic
}
class Derived : public Base
{
// Even more magic
}
class Foo
{}
// Is it possible to tell template to accept only classes derived from Base?
template<class T>
do_something(T obj)
{
// Perform some dark magic incantations on obj
}
int main()
{
Foo foo;
Derived derived;
do_something<Derived>(derived); // Normal
do_something<Foo>(foo); // Compilation error if I understand templates correctly
}
最佳答案
C++20 之前的版本您可以使用 enable_if
加上检查is_base_of
:
template<class T, std::enable_if_t<std::is_base_of_v<Base, T> && !std::is_same_v<Base, T>, int> = 0>
void do_something(T obj)
{
// Perform some dark magic incantations on obj
}
请注意,我已明确禁止该类型成为 Base
的实例(因为 is_base_of
认为类型是其自身的基础)。如果您想允许 Base
的实例,然后删除 && !std::is_same_v<Base, T>
Live Demo
在 C++20 中,我们几乎可以直接翻译 enable_if
进入requires
表达式:
template<class T>
requires (std::is_base_of_v<Base, T> && !std::is_same_v<Base, T>)
void do_something(T obj)
{
// ...
}
Concepts Demo
或者,如果您想允许 Base
的实例,您可以使用内置的derived_from
概念:
template<class T>
requires std::derived_from<T, Base>
void do_something(T obj)
{
// ...
}
Concepts Demo 2
关于C++ - 使用基类作为模板参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65570033/