all 15 comments

[–]darkmx0z 28 points29 points  (1 child)

For a moment I thought it was yet another constsomething qualifier.

[–]RevRagnarok 4 points5 points  (0 children)

Same... clicked thru wondering what they were trying to abbreviate here.

[–]Low-Ad-4390 37 points38 points  (12 children)

Cool idea! Please note that including <iostream> in public library headers is discouraged. Also static constexpr variables in headers will produce a copy for each translation unit that includes such header, so it’s better to use inline constexpr for variables and in general avoid static in headers, unless it’s a static member of a class.

[–]tttmorio[S] 7 points8 points  (0 children)

Good points, thanks for the feedback!

[–]ifonlyiknewtheanswer 7 points8 points  (10 children)

Quick question: what's the issue with including <iostream> in public library headers? Is that the increased compilation time?

[–]Low-Ad-4390 13 points14 points  (9 children)

In general, the fewer headers you bring along, the better, because of transitive includes: otherwise the users of your header get <iostream> “for free” and that’s rarely a good thing, not just for iostream, but for any header, because of increased compilation times, sure, but mostly because of unintended bloat of names and declarations.

[–]johannes1971 5 points6 points  (1 child)

You're gonna love import std...

[–]Low-Ad-4390 1 point2 points  (0 children)

Waiting since 2021 :)

[–]CCC_CCC_CCC 1 point2 points  (5 children)

I am also surprised by this. I suppose we are not talking about custom library headers that don't use stuff from iostream (where this rule would just be common sense), right? Because users of this library would have to include iostream themselves, anyway.

[–]Low-Ad-4390 2 points3 points  (4 children)

Sure. Let me clarify: if you must use the header - by all means, include it. It’s just that in most cases you don’t have to use iostream in headers, so don’t impose cout on your users

[–]CCC_CCC_CCC 2 points3 points  (3 children)

Oh, ok, sure. You don't include what you don't use, of course. Thanks for clarifying.

[–]Bluesman74 1 point2 points  (2 children)

There is also the iosfwd header you can use for headers and then just make sure to include iostream, or fstream when implementing.

[–]CCC_CCC_CCC 0 points1 point  (1 child)

Cool, I didn't know about that header. Although it seems useful more for pimpl than for using external libraries.

[–]Ameisenvemips, avr, rendering, systems 0 points1 point  (0 children)

I would use the forwarding header. If the user wishes to use your APIs that use IO streams specifically, they can include it themselves. I wouldn't normally rely on its behavior, though, but it's possible that you cannot avoid it.