
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?

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@lists.boost.org [mailto:boost-users-bounces@lists.boost.org] Em nome de Krzysztof Zelechowski Enviada em: quarta-feira, 21 de setembro de 2011 11:59 Para: boost-users@lists.boost.org 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@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users

Matheus Peschke de Azevedo wrote:
Krzysztof,
According to the C++ standard, the ostream output (cout and wcout) will only handle built-in types (e.g., int, char, double, etc).
Not true, vide ::std:: complex.
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; }
This is strictly forbidden by the standard AFAIK. Chris

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; }
This is strictly forbidden by the standard AFAIK.
How can the standard forbid defining your own operators? The standard forbids putting things into std namespace.

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; }
This is strictly forbidden by the standard AFAIK.
Chris
It's forbidden if the overload is placed inside namespace std. Placing it in a different namespace (including the global namespace) is perfectly OK, AFAIK. Nate

On Wed, 21 Sep 2011 16:59:06 +0200, Krzysztof Żelechowski <giecrilj@stegny.2a.pl> wrote:
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?
If you can replace std::pair with boost::tuple, you could write (cout << make_tuple (0, 1)). Boris
participants (5)
-
Boris Schaeling
-
Igor R
-
Krzysztof Żelechowski
-
Matheus Peschke de Azevedo
-
Nathan Ridge