Rename Library Names

Excerpt from Exceptional C ++:

"In the old days, you could just replace" #include "with" class ostream; "in this situation, because ostream used to be a class, and it was not in the std namespace. Alas, no more. class ostream;" is illegal for two reasons:

ostream is now in the std namespace, and programmers are not allowed to declare anything that lives in the std namespace.

ostream is now a typedef template; in particular, it is typedef'd as basic_ostream. Not only would the basic_ostream template be messy for sending-declaring anyway, but you couldnโ€™t reliably redirect-declare it at all, because library implementations are allowed to do things like add your own additional template parameters (beyond those required by the standard ), which, of course, your code would not know about - one of the main reasons for the rule that programmers are not allowed to write their own declarations for things in the std namespace. "

My question is:

I do not understand the part in bold.

Thanks,

0
source share
2 answers

The bold part just says that you cannot forward declore ostream like this:

class ostream; 
  • because ostream is now a typedef, and the details of the declaration may or may not vary across implementations.
  • because programmers are not allowed to declare anything in the std namespace (although it will work on most compilers).

If you want an ostream forward declaration, include <iosfwd> instead. (Or look how it looks for your implementation).

+5
source

You can, if you're okay with the fact that it will only work for a specific version of a particular compiler. It will be ugly, but you can do it (you can pretty much copy the contents of the header into your code, and it will still work).

The reason you shouldn't, that's why it is in the header. You want to use some kind of external interface and do not want to worry about the internal components of the compiler. The compiler guarantees you an external interface. As for the implementation, the choice of compilers.

+1
source

Source: https://habr.com/ru/post/886031/


All Articles