Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where is cout declared?

My computer science professor wants us to find the declaration of cout. I've compiled a simple Hello world program using g++ and the -E parameter. Here's what my hello.cpp looks like:

#include <iostream>

using namespace std;

int main(){

  string name="";

  cout << "Good morning! What's your name?";

  cin >> name;

  cout << "Hello " << name << ".\n";

  return 0; 

}

My compile command:

g++ -E hello.cpp > hello.p

In hello.p, I ran a search in VIM, like so:

:/cout

I see the following line:

extern ostream cout;

Is that the declaration of cout, and is cout an instance of the ostream class?

Edit:

What's the wcout declaration there for? If I recall correctly the letter "w" stands for "wide", but I don't know what implication that has. What is a wcout and a wostream?

like image 219
Moshe Avatar asked Mar 11 '12 04:03

Moshe


People also ask

Is cout a declaration?

C++ std:cout: A namespace is a declarative region inside which something is defined. So, in that case, cout is defined in the std namespace. Thus, std::cout states that is cout defined in the std namespace otherwise to use the definition of cout which is defined in std namespace.

What library includes cout?

The cout command is a data stream which is attached to screen output and is used to print to the screen, it is part of the iostream library.

How do you declare cout in scope?

Use std::cout , since cout is defined within the std namespace. Alternatively, add a using std::cout; directive.


1 Answers

Yes, that is indeed the declaration of std::cout, found inside the <iostream> header.

The relevant standard part can be found in §27.4.1 [iostream.objects.overview]:

Header <iostream> synopsis

#include <ios>
#include <streambuf>
#include <istream>
#include <ostream>

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;
}

p1 The header <iostream> declares objects that associate objects with the standard C streams provided for by the functions declared in <cstdio> (27.9.2), and includes all the headers necessary to use these objects.

like image 118
Xeo Avatar answered Oct 23 '22 13:10

Xeo