相关疑难解决方法(0)

根据转储成员函数的operator <<函数的通用实现

我的所有类都实现了一个dump成员函数,例如:

struct A {
    template <typename charT>
    std::basic_ostream<charT> &
    dump(std::basic_ostream<charT> &o) const {
        return (o << x);
    }
    int x = 5;
};
Run Code Online (Sandbox Code Playgroud)

我想operator<<为所有这样的类实现一次函数:

template<typename charT, typename T>
std::basic_ostream<charT> &
operator<< (std::basic_ostream<charT> &o, const T &t) {
    return t.dump(o);
}
Run Code Online (Sandbox Code Playgroud)

问题是此模板捕获了所有类型,包括标准类型.有办法解决这个问题吗?

c++

5
推荐指数
1
解决办法
132
查看次数

标签 统计

c++ ×1