I have a base class Base
, that a lot of other classes will be derived from. I would like to define:
template<typename Derived>
ostream &operator<< (ostream &o, Derived &derived) {
}
But only for classes derived from Base
. I need all previously defined operator<<
to be used for other types. How to do that? Is that possible?
I cannot create ostream &operator<< (ostream &o, Base &base)
, because I need the exact type to be used in some type traits. Is there any way to "push" the derived type while passing the value as a base type?
http://www.boost.org/doc/libs/1_46_0/libs/utility/enable_if.html
http://www.boost.org/doc/libs/1_42_0/libs/type_traits/doc/html/boost_typetraits/reference/is_base_of.html
template<typename Derived>
typename enable_if<is_base_of<Base, Derived>, ostream&>::type
operator<< (ostream &o, Derived &derived)
{
}
You can use type traits and SFINAE to let only classes derived from Base into your function:
#include <iostream>
#include <boost/utility/enable_if.hpp>
#include <boost/type_traits/is_base_and_derived.hpp>
struct Base {};
template<typename Derived>
typename boost::enable_if<boost::is_base_and_derived<Base, Derived>, std::ostream&>::type
operator<<(std::ostream& o, Derived& derived);
struct A : Base {};
struct B : Base {};
struct C {};
int main()
{
A a;
B b;
C c;
std::cout << a << '\n'; // compiles
std::cout << b << '\n'; // compiles
std::cout << c << '\n'; // error: no match for 'operator<<' in 'std::cout << c'
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With