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
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
: template
std::basic_ostream<CharType>& operator<< (std::basic_ostream<CharType>& o, std::pair 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
: template
std::basic_ostream<CharType>& operator<< (std::basic_ostream<CharType>& o, std::pair 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
: template
std::basic_ostream<CharType>& operator<< (std::basic_ostream<CharType>& o, std::pair 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
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