Boost logo

Boost Users :

Subject: [Boost-users] RES: How do I print a standard pair?
From: Matheus Peschke de Azevedo (mpeschke_at_[hidden])
Date: 2011-09-21 17:06:02


Krzysztof,

According to the C++ standard, the ostream output (cout and wcout) will only
handle built-in types (e.g., int, char, double, etc). It's up to the
developer implement how all other types will print to the output. See full
rationale and details in Stroustrup, Bjarne TCPPPL 3rd Edition - 21.2.2
Output of Built In Types.

In short, you'll have to define a global operator<< for type std::pair<T,
U>:

template<typename CharType, typename T, typename U>
std::basic_ostream<CharType>& operator<< (std::basic_ostream<CharType>& o,
std::pair<T, U> mypair)
{
        return o << "This is my way of printing a pair: first: " <<
mypair.first << " and second: " << mypair.second;
}

Be advised that types T and U must be either built-in types or types with
defined operator<<.

Cheers,
Matheus

-----Mensagem original-----
De: boost-users-bounces_at_[hidden]
[mailto:boost-users-bounces_at_[hidden]] Em nome de Krzysztof
Zelechowski
Enviada em: quarta-feira, 21 de setembro de 2011 11:59
Para: boost-users_at_[hidden]
Assunto: [Boost-users] How do I print a standard pair?

The expression (cout << make_pair (0, 1)) gives me an error. Does Boost
offer me a way to print a (rvalue) pair, as within an expression?

_______________________________________________
Boost-users mailing list
Boost-users_at_[hidden]
http://lists.boost.org/mailman/listinfo.cgi/boost-users


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net