仅为从单个基础派生的类定义模板函数

Jur*_*aho 8 c++ templates

我有一个基类Base,很多其他类将派生自.我想定义:

template<typename Derived>
ostream &operator<< (ostream &o, Derived &derived) {
}
Run Code Online (Sandbox Code Playgroud)

但仅适用于派生自的类Base.我需要以前定义的所有内容operator<<用于其他类型.怎么做?那可能吗?

我无法创建ostream &operator<< (ostream &o, Base &base),因为我需要在某些类型特征中使用的确切类型.有什么方法可以在将值作为基类型传递时"推送"派生类型?