[英]overload operator<< for class template
let's take this code to implement the operator<< for two classes: 让我们以这段代码为两个类实现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;
}
Because I am lazy I'd like to provide a template version of operator<< instead of the two versions as above. 因为我很懒,所以我想提供operator <<的模板版本,而不是上面的两个版本。 I can do it easily replacing with:
我可以轻松地替换为:
template< class T>
std::ostream& operator<<(std::ostream& os, const T& in)
{
return in.print(os);
}
So far so good. 到现在为止还挺好。 If I have several classes I can implement the operator<< in one go.
如果我有几个类,则可以一次性实现operator <<。 The trouble start when one of my classes is a class template.
当我的一个班级是班级模板时,麻烦就开始了。 Let's take the previous example but with B class template:
让我们以前面的示例为例,但使用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;
}
This version works and I have the expected result, however I have provided two operator<< functions (one for each class), let's imagine that I have 200 classes that already implement a public ostream& print(ostream& os) const. 这个版本可以正常工作,并且达到了预期的效果,但是我提供了两个operator <<函数(每个类一个),让我们假设我有200个已经实现了公共ostream&print(ostream&os)const的类。 Some of them are template class (with also multiple parameters).
其中一些是模板类(还有多个参数)。
How can I write a template version of the operator<< in this scenario? 在这种情况下,如何编写operator <<的模板版本?
Thanks for you help. 谢谢你的帮助。
Same as above: 与上述相同:
template< class T>
std::ostream& operator<<(std::ostream& os, const T& in)
{
return in.print(os);
}
However, a "catch all" overload like that is a bit like dynamite fishing. 但是,像这样“全部捕获”的过载有点像炸药捕鱼。 You can constrain the range of the operator to all T's which define a suitable "print" member using SFINAE (http://en.wikipedia.org/wiki/Substitution_failure_is_not_an_error):
您可以使用SFINAE(http://en.wikipedia.org/wiki/Substitution_failure_is_not_an_error)将运算符的范围限制为所有T,这些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;
}
This effectively disables the operator<< when searching for a suitable overload, if T has no member print(std::ostream&)
如果T 没有成员
print(std::ostream&)
,这将在搜索合适的重载时有效地禁用operator <<
This is, actually, what the Concepts
were intended for. 实际上,这就是这些
Concepts
的目的。 You can emulate them with Boost.Concepts at the moment. 您现在可以使用Boost.Concepts模仿它们。
However, there is one issue with your solution: Argument Dependent Lookup. 但是,您的解决方案存在一个问题:参数依赖查找。
When you use an operator, it need be: 使用运算符时,需要:
However if you define your template overload, it cannot be present in the namespace of all those other classes. 但是,如果定义模板重载,则它不能出现在所有其他类的名称空间中。
I suggest cheating . 我建议作弊 。
If you wrap std::ostream&
in a class of your own, you can then, in its namespace, provide all the operator overloads that you wish for: 如果将
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
You can then add opportunistic overloads for common types: 然后可以为常见类型添加机会重载:
inline MyStream& operator<<(MyStream& s, bool b)
{
s._o << (b ? 'Y' : 'N');
return s;
}
Without risking a clash with functions defined in std
. 不用冒与
std
定义的函数发生冲突的风险。
Note that it trades reworking the class hierarchy (having a common PrintableInterface
would be great too) vs reworking the calls. 请注意,与重新构造调用相比,它可以重新构造类层次结构(具有通用的
PrintableInterface
也很好)。 The latter can be done with a search and replace though. 后者可以通过搜索和替换来完成。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.