Allow stream operator on derived classes


I have an interface that boils down to

class interface { protected: virtual void write(std::string const &) = 0; };

And derived classes like

class derived : public interface { protected: void write(std::string const & buf) { std::cout << buf << std::endl; } };

In my application, these objects are passed around as smart pointers, i.e. std::shared_ptr<derived>. I hoped I could overload the << operator, but only for smart pointer of derivatives of my interface. I tried this:

class interface { /* ... */ private: template <typename Derived> friend typename std::enable_if< std::is_base_of<interface, Derived>::value, std::shared_ptr<Derived> >::type & operator<<(std::shared_ptr<Derived> & lhs, std::string const & rhs) { lhs->write(rhs); return lhs; } };

But when I try std::shared_ptr<derived> sp; sp << "test";, the compiler complains that virtual void derived::write(const string&) is protected within this context (this context is my friend function).

Is there a way to achieve this without redundantly writing a stream operator for every derived class?


Why not simply define your operator as:

friend std::shared_ptr<interface> &operator<<(std::shared_ptr<interface> & lhs, std::string const & rhs);

and pass your objects as std::shared_ptr<interface>?


  • Use Prism.Unity.UnityBootstrapper with Unity.IUnityContainer
  • How do I change the Report Designer Ruler unit from Inches to centimetres?
  • Swift check if value is of type array (of any type)
  • TeamCity NuGet package build for different .NET Frameworks
  • How to have an email link inside a text in Xaml?
  • F# slicing array by indices array
  • APNs messages are delivered but not received on iOS device
  • PHP socket_read() only gets the first byte of the stream
  • Need help to stop program terminating without users consent
  • Iterating over a container bidirectionally
  • Rx produce and consume on different threads
  • java.net.SocketTimeoutException on embedded tomcat with jersey
  • What's the essential difference between these two variadic functions?
  • std::system Exception when instantiating a singleton object
  • OpenMP and File I/O
  • c++ regex_replace not doing intended substitution
  • I am receiving HibernateException “No Hibernate Session bound to thread, and configuration does not
  • Reduction and collapse clauses in OMP have some confusing points
  • Date Conversion from yyyy-mm-dd to dd-mm-yyyy
  • Debug.DrawLine not showing in the GameView
  • copying resource to sdcard gives a damaged file in android
  • why overloaded new operator is calling constructor even I am using malloc inside overloading functio
  • CSS Linear-gradient formatting issue accross different browsers
  • C# - Serializing and deserializing static member
  • Sending data from AppleScript to FileMaker records
  • How to include full .NET prerequisite for Wix Burn installer
  • JTable with a ScrollPane misbehaving
  • Is it possible to post an object from jquery to bottle.py?
  • unknown Exception android
  • Easiest way to encapsulate a HTML5 webpage into an android app?
  • Busy indicator not showing up in wpf window [duplicate]
  • costura.fody for a dll that references another dll
  • Observable and ngFor in Angular 2
  • How to Embed XSL into XML
  • failed to connect to specific WiFi in android programmatically
  • UserPrincipal.Current returns apppool on IIS
  • Conditional In-Line CSS for IE and Others?
  • Python/Django TangoWithDjango Models and Databases
  • java string with new operator and a literal
  • How can I use threading to 'tick' a timer to be accessed by other threads?