过载运算符<<用于课堂模板

时间:2011-02-07 16:19:49

标签: c++ templates

让我们用这段代码来实现运算符<<两个班级:

#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;
}

因为我很懒,所以我想提供运营商的模板版本&lt;&lt;而不是上面的两个版本。我可以轻松地替换为:

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

到目前为止一切顺利。如果我有几个类,我可以实现运算符&lt;&lt;一气呵成。当我的一个类是类模板时,麻烦就开始了。让我们采用前面的例子,但使用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;
}

此版本有效且我有预期的结果,但是我提供了两个运算符&lt;&lt;函数(每个类一个),让我们假设我有200个类已经实现了公共ostream&amp; print(ostream&amp; os)const。其中一些是模板类(也有多个参数)。

如何编写运算符的模板版本&lt;&lt;在这种情况下?

谢谢你的帮助。

2 个答案:

答案 0 :(得分:7)

与上述相同:

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

然而,像这样的“赶上所有”超载有点像炸药钓鱼。您可以使用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;
}

这有效地禁用了运算符&lt;&lt;在搜索合适的重载时,如果T有成员print(std::ostream&)

答案 1 :(得分:0)

实际上,这是Concepts的目的。您现在可以使用Boost.Concepts模拟它们。

但是,您的解决方案存在一个问题:Argument Dependent Lookup。

使用运算符时,需要:

  • 在当前范围内(搜索将向外辐射以考虑越来越多的全局范围)
  • 或其中一个参数的命名空间。

但是,如果您定义模板重载,则它不能出现在所有其他类的命名空间中。

我建议作弊

如果将std::ostream&包装在自己的类中,则可以在其命名空间中提供所需的所有运算符重载:

namespace X {

struct MyStream
{
  MyStream(std::ostream& o): _o(o) {}
  std::ostream& _o;
};

template <typename T>
MyStream& operator<<(MyStream& s, T const& t)
{
  t.print(s._o);
  return s;
}

} // namespace X

然后,您可以为常见类型添加机会重载:

inline MyStream& operator<<(MyStream& s, bool b)
{
  s._o << (b ? 'Y' : 'N');
  return s;
}

不存在与std中定义的函数发生冲突的风险。

请注意,它会重新处理类层次结构(具有共同的PrintableInterface也会很好)与重新调用调用。后者可以通过搜索完成,然后替换。

相关问题