On 30 Apr, 17:26, Niels Dekker - no return address
<nore...@[EMAIL PROTECTED]
> wrote:
> Tony Guinness wrote:
^^^^^^^^^^^^^
(nice assumption, but wrong - my surname isn't "Guinness";
"Guinness Tony" is a monicker used by my familiars to
distinguish me from those Tonys that don't imbibe the
Black Stuff!)
> > The operator<< that is called here...
> >> std::string s = (std::ostringstream() << "i = " << i).str();
> > is member function std::ostream::operator<<(char const*). There is no
> > case of a tem****ary being bound to a non-const reference here. It is
> > perfectly legal and should compile just fine.
>
> STL****t has it defined as a non-member!
>
> template <class _Traits>
> inline basic_ostream<char, _Traits>& _STLP_CALL
> operator<<(basic_ostream<char, _Traits>& __os, const char* __s)
>
> Just look at "stl****t/stl/_ostream.h",
fromhttp://www.stl****t.com/archive/STL****t-5.0RC2.tar.gz
My mistake. I really should consult the Standard before making such
rash claims. The C-style string insertion operators are, indeed,
defined as free function templates in [lib.ostream]27.6.2.1:
namespace std {
...
template<class charT, class traits>
basic_ostream<charT,traits>&
operator<<(basic_ostream<charT,traits>&,
const charT*);
template<class charT, class traits>
basic_ostream<charT,traits>&
operator<<(basic_ostream<charT,traits>&,
const char*);
// partial specializationss
template<class traits>
basic_ostream<char,traits>& operator<<(basic_ostream<char,traits>&,
const char*);
// signed and unsigned
template<class traits>
basic_ostream<char,traits>& operator<<(basic_ostream<char,traits>&,
const signed char*);
template<class traits>
basic_ostream<char,traits>& operator<<(basic_ostream<char,traits>&,
const unsigned char*);
}
[how odd - I've never noticed that "specializationss" typo before!]
Sorry to have added unnecessary confusion to the discussion.
Nevertheless, your example still compiles under the toolsets I have
to hand...
int i = 42;
std::string s = static_cast<std::ostringstream&>(
std::ostringstream() << "i = " << i).str();
....but, on execution, I find that the operator<< chosen by the
compiler
is std::ostream::operator<<(const void*), which /is/ a member function
(I checked this time!) The text written into the stringstream's
string buffer is not a copy of the string literal, but a
representation
of /the address of/ that string literal.
The STL****t implementation that you cited also has the (const void*)
inserter as a member function, so I'm still perplexed as to why your
compiler baulked at it.
In conclusion, the "flush trick" I presented in my previous post is
also required when the first item in the insert-chain is a C-style
string (including string literals), i.e.
int i = 42;
std::string s = static_cast<std::ostringstream&>(
std::ostringstream() << std::flush << "i = " << i).str();
Cheers,
Tony.
--
[ See http://www.gotw.ca/resources/clcm.htm
for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


|
30 Posts in Topic:
|
"Niels Dekker - no r |
2008-04-23 13:14:37 |
|
Martin York <Martin.Yo |
2008-04-24 01:05:54 |
|
Peter Jones <dev-null@ |
2008-04-24 01:10:33 |
|
Carl Barron <cbarron41 |
2008-04-24 01:43:21 |
|
Vidar Hasfjord <vattil |
2008-04-24 15:12:53 |
|
Brendan <catphive@[EMA |
2008-04-24 15:43:20 |
|
Brendan <catphive@[EMA |
2008-04-25 10:22:59 |
|
Niels Dekker - no return |
2008-04-26 08:46:24 |
|
Carl Barron <cbarron41 |
2008-04-27 07:13:20 |
|
Brendan Miller <catphi |
2008-04-27 07:29:25 |
|
Vidar Hasfjord <vattil |
2008-04-27 07:29:42 |
|
Vidar Hasfjord <vattil |
2008-04-28 01:56:19 |
|
Vidar Hasfjord <vattil |
2008-04-28 02:13:22 |
|
vova777@[EMAIL PROTECTED] |
2008-04-28 16:06:27 |
|
"Niels Dekker - no r |
2008-04-28 16:01:35 |
|
guinness.tony@[EMAIL PROT |
2008-04-28 16:01:50 |
|
"Hendrik Schober&quo |
2008-04-28 20:35:47 |
|
Vidar Hasfjord <vattil |
2008-04-29 10:38:48 |
|
Brendan Miller <catphi |
2008-04-29 10:43:23 |
|
Vidar Hasfjord <vattil |
2008-04-29 10:43:23 |
|
Vidar Hasfjord <vattil |
2008-04-29 10:43:22 |
|
vova777@[EMAIL PROTECTED] |
2008-04-29 12:13:56 |
|
Vidar Hasfjord <vattil |
2008-04-29 17:41:45 |
|
Niels Dekker - no return |
2008-04-30 10:26:51 |
|
vova777@[EMAIL PROTECTED] |
2008-04-30 10:39:18 |
|
Vidar Hasfjord <vattil |
2008-04-30 17:04:43 |
|
guinness.tony@[EMAIL PROT |
2008-05-01 13:12:58 |
|
"Niels Dekker - no r |
2008-05-02 12:45:30 |
|
guinness.tony@[EMAIL PROT |
2008-05-07 11:43:29 |
|
"Niels Dekker - no r |
2008-05-08 21:29:05 |
|