如何为C++中的所有派生类专门化模板函数?

问题描述 投票:0回答:1

我有代码:

class Base {};

template <T>
void Print(const T& obj);

我想专门针对

Print
派生自
Base
的所有类。

我尝试了

concept

template <class T>
concept DerivedFromBase = std::is_base_of_v<Base, T>;

template <DerivedFromBase T>
void Print<T>(…) { … }

但是编译器说它是部分专业化 - 而函数不能拥有它。

还有哪些其他解决方案和途径?

c++ templates c++-concepts
1个回答
0
投票

只需删除

<T>
就足够了:

template <class T>
concept DerivedFromBase = std::is_base_of_v<Base, T>;

template <DerivedFromBase T>
void Print(…) { … }
© www.soinside.com 2019 - 2024. All rights reserved.