重载operator <<用于类模板

Ale*_*zzi 2 c++ templates

让我们用这段代码实现两个类的operator <<:

#include <iostream>

using std::cout;
using std::endl;

class A
{
    int a1_;
public:
    A(int a1) : a1_(a1){}
    std::ostream& print(std::ostream& os) const
    {
        return os << "a1_ : " << a1_ << endl;
    }
};

class B
{
    int b1_;
    double b2_;
public:
    B(int b1,double b2) : b1_(b1),b2_(b2){}
    std::ostream& print(std::ostream& os) const
    {
        os << "b1_ : " << b1_ << endl;
        os << "b2_ : " << b2_ << endl;
        return os;
    }
};

std::ostream& operator<<(std::ostream& os, const A& in)
{
    return in.print(os);
}

std::ostream& operator<<(std::ostream& os, const B& in)
{
    return in.print(os);
}

int main(int argc,char* argv[])
{
    A myA(10);
    B myB(20,30.14);

    cout << myA << myB << endl;
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

因为我很懒,所以我想提供operator <<的模板版本,而不是上面提到的两个版本.我可以轻松地替换为:

template< class T>
std::ostream& operator<<(std::ostream& os, const T& in)
{
    return in.print(os);
}
Run Code Online (Sandbox Code Playgroud)

到现在为止还挺好.如果我有几个类,我可以一次实现运算符<<.当我的一个类是类模板时,麻烦就开始了.让我们采用前面的例子,但使用B类模板:

#include <iostream>

using std::cout;
using std::endl;

class A
{
    int a1_;
public:
    A(int a1) : a1_(a1){}
    std::ostream& print(std::ostream& os) const
    {
        return os << "a1_ : " << a1_ << endl;
    }
};

template <class T>
class B
{
    int b1_;
    T b2_;
public:
    B(int b1,T b2) : b1_(b1),b2_(b2){}
    std::ostream& print(std::ostream& os) const
    {
        os << "b1_ : " << b1_ << endl;
        os << "b2_ : " << b2_ << endl;
        return os;
    }
};


std::ostream& operator<<(std::ostream& os, const A& in)
{
    return in.print(os);
}

template <class T>
std::ostream& operator<<(std::ostream& os, const B<T>& in)
{
    return in.print(os);
}

int main(int argc,char* argv[])
{
    A myA(10);
    B<A> myB(20,myA);

    cout << myA << myB << endl;
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

这个版本工作,我有预期的结果,但是我提供了两个操作符<<函数(每个类一个),让我们假设我有200个类已经实现了公共ostream&print(ostream&os)const.其中一些是模板类(也有多个参数).

如何在此场景中编写运算符<<的模板版本?

谢谢你的帮助.

Nor*_*ame 7

与上述相同:

template< class T>
std::ostream& operator<<(std::ostream& os, const T& in)
{
    return in.print(os);
}
Run Code Online (Sandbox Code Playgroud)

然而,像这样的"捕捉所有"超载有点像炸药捕鱼.您可以使用SFINAE(http://en.wikipedia.org/wiki/Substitution_failure_is_not_an_error)将操作符的范围限制为定义合适的"打印"成员的所有T的范围:

template<int X, typename T>
struct enabler
{
    typedef T type;
};
template<class T>
typename enabler< sizeof(&T::print), std::ostream&>::type
operator << (std::ostream &o, const T &t)
{
    t.print(o);
    return o;
}
Run Code Online (Sandbox Code Playgroud)

如果T 没有成员,这会在搜索合适的重载时有效地禁用operator <<print(std::ostream&)