lundi 20 juin 2022

What's the relation between `std::cout` and `std::ostream`?

I saw the code snippet below somewhere.

#include <iostream>


int main()
{
    std::ostream& os = std::cout;

    os << "thanks a lot" << std::endl;
    return 0;
}

Since the aforementioned code snippet works well, it indicates that std::cout is derived from std::ostream. But I can't find any direct reference yet.

As per the document, which says that[emphasis mine]:

The global objects std::cout and std::wcout control output to a stream buffer of implementation-defined type (derived from std::streambuf), associated with the standard C output stream stdout.

The above quotation says that std::cout controls ouput to a type which derived from std::streambuf other than std::cout derived from std::streambuf.

And I only find the declaration of std::cout in a file named /usr/include/c++/7/iostream:

  extern ostream cout;      /// Linked to standard output

I can't find the implementation of std::cout.

Aucun commentaire:

Enregistrer un commentaire