Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The standard Namespace problem

Tags:

c++

Hii ,

I am relatively new to programming C++ . I do know that the functions cout , cin etc... are defined in the standard name space . But we also include iostream header file for running the program .

So , is it like

 namespace std
   {

     declaration of cout 

     declaration of cin 

     ..... some other declarations etc....

   }

and theior actual implementations inside istream and ostream ... ????

Or , is it the other way round ...??? like ....

namespace std 
   {
     complete definition of cout 
     complete definition of cin  
     .........

   } 

and their signatures are just placed in the iostream file like ...

iostream file 
  {
    std :: cout 
    std :: cin 
    .....

  }

Please provide any examples or links that you might think will help me understand better

like image 645
Flash Avatar asked May 19 '26 06:05

Flash


2 Answers

I do know that the functions cout , cin etc... are defined in the standard name space.

These are not really functions, but global instances of basic_ostream and basic_istream.

But we also include iostream header file for running the program.

You rather include headers so you can compile your source (the compiler needs to declarations etc).

The rest of the question is rather fuzzy. How the standard library is implemented is pretty much up to the implementation. The standard requires that if you include iostream, you will get the declarations of the following globals:

namespace std {
  extern istream cin;
  extern ostream cout;
  extern ostream cerr;
  extern ostream clog;

  extern wistream wcin;
  extern wostream wcout;
  extern wostream wcerr;
  extern wostream wclog;
}
like image 198
UncleBens Avatar answered May 21 '26 18:05

UncleBens


The standard really doesn't say. It's entirely possible for the implementer to do it as a header-only library, but it's much more likely for them to just put the declarations in headers and put the implementations in the CRT.

EDIT: However, the definitions for cin, cout, etc need to be extern and defined in some sort of library. (See UncleBens' answer)

like image 25
Billy ONeal Avatar answered May 21 '26 20:05

Billy ONeal



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!