On Jun 11, 2015, at 10:25, Anton Bachin
wrote: I’m planning to overload the stream operators, but probably in a separate header file. The reason is that including <iostream> slows down compilation by an order of magnitude or so compared to just the current contents of enum.h, when used for a few enums. Do you think I should just not bother with this, and include it anyway?
<iosfwd> should be enough, I think.
Perfect, this is the kind of thing I was hoping for. Thanks!
Well. <iosfwd> turned out to be not so useful in a header-only library. Including it allows you to declare overloads, but not to (easily) define them, because you need various iostreams definitions and additional declarations. I did timing tests on some headers, and including anything from iostreams except <iosfwd> carried a significant penalty, well within a factor of 2 of <iostream> itself. I may have to move the operators to a separate file, but for now I put their definitions in enum.h, made them into templates, and hid the undefined iostreams types behind templates that depend on the overall template parameter. This prevents the compiler from checking the types in the operator definitions unless and until the operators actually get used. It looks very ridiculous. It does, however, keep everything in one header, with no penalty for those not using iostreams. Has anyone seen a better way? https://github.com/aantron/better-enums/blob/0f636671/enum.h#L1044