Namespaces
Variants
Views
Actions

std::cout, std::wcout

From cppreference.com
< cpp‎ | io
Revision as of 16:14, 19 April 2012 by P12bot (Talk | contribs)

Template:cpp/io/basic ostream/sidebar Template:ddcl list begin <tr class="t-dsc-header">

<td>
Defined in header <iostream>
</td>

<td></td> <td></td> </tr> <tr class="t-dcl ">

<td >
extern std::ostream cout;
</td>

<td > (1) </td> <td class="t-dcl-nopad"> </td> </tr> <tr class="t-dcl ">

<td >
extern std::wostream wcout;
</td>

<td > (2) </td> <td class="t-dcl-nopad"> </td> </tr> Template:ddcl list end

The global objects Template:cpp and Template:cpp control output to a stream buffer of implementation-defined type (derived from Template:cpp), associated with the standard C output stream Template:cpp.

These objects are guaranteed to be constructed before the first constructor of a static object is called and they are guaranteed to outlive the last destructor of a static object, so that it is always possible to write to Template:cpp in user code.

Unless Template:cpp has been issued, it is safe to concurrently access these objects from multiple threads for both formatted and unformatted output.

Once initialized, Template:cpp is tie()'d to Template:cpp and Template:cpp is tie()'d to Template:cpp, meaning that any input operation on Template:cpp executes Template:cpp (via Template:cpp's constructor).

Once initialized, Template:cpp is also tie()'d to Template:cpp and Template:cpp is tie()'d to Template:cpp,, meaning that any output operation on Template:cpp executes Template:cpp (via Template:cpp's constructor) (since C++11)

Example

#include <iostream>
struct Foo {
    int n;
    Foo() {
       std::cout << "static constructor\n";
    }
    ~Foo() {
       std::cout << "static destructor\n";
    }
};
Foo f; // static object
int main()
{
    std::cout << "main function\n";
}

Output:

static constructor
main function
static destructor

See also

Template:cpp/io/ios base/dcl list InitTemplate:cpp/io/dcl list clogTemplate:cpp/io/dcl list cout