Reputation: 3978
let's take this code to implement the operator<< for two classes:
#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. 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. The trouble start when one of my classes is a class template. Let's take the previous example but with B class template:
#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. Some of them are template class (with also multiple parameters).
How can I write a template version of the operator<< in this scenario?
Thanks for you help.
Upvotes: 2
Views: 1423
Reputation: 299810
This is, actually, what the Concepts
were intended for. You can emulate them with Boost.Concepts at the moment.
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:
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
.
Note that it trades reworking the class hierarchy (having a common PrintableInterface
would be great too) vs reworking the calls. The latter can be done with a search and replace though.
Upvotes: 0
Reputation: 28737
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):
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&)
Upvotes: 7