Skip to content

Add overloads of operator<< for idiomatic C++ printing

HepMC3 currently uses a Print class to generate a character representation of its objects. This works, but this is not idiomatic C++. In idiomatic C++, one writes an overload of operator<< for streamable objects:

inline std::ostream& operator<<(std::ostream& os, const some_class& x) { … }

This declaration/implementation should be put into an optional header, so that users who want to implement their own operator<< can do so without causing conflicts with the library implementation.

Clearly, this is not adding new functionality and therefore not an urgent thing, but it would make printing the C++ way possible:

auto my_particle = GenParticle(…);
std::cout << "this is my particle: " << my_particle << std::endl;

Supporting idiomatic C++ generally makes a library easier to use, because common patterns work. It also would make the HepMC3 objects printable by third-party formatting libraries, which use operator<< as well. Some classes, like boost::variant detect whether all bounded types are streamable and only then make the whole variant streamable.

Edited by Hans Peter Dembinski