473,397 Members | 2,028 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 software developers and data experts.

Creating a stringstream in a parameter

I have a base class that takes a string to its constructor. I want to
construct a concrete class that does this:

class concrete: public base
{
public:
concrete() : base( /* create a stringstream here and convert to
string */ ){}
};

However, I can't get it to work. I can pass this:

ostringstream("String").str()

but not this:

(ostringstream("String") << 3).str()

or this:

((ostringstream)(ostringstream("String") << 3)).str()

which is what I want to do (as I need to build the string on the fly.

What do I do?

Jan 3 '06 #1
6 4538
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

an**************@googlemail.com wrote:
I have a base class that takes a string to its constructor. I want to
construct a concrete class that does this:

class concrete: public base
{
public:
concrete() : base( /* create a stringstream here and convert to
string */ ){}
};

However, I can't get it to work. I can pass this:

ostringstream("String").str()

but not this:

(ostringstream("String") << 3).str()

or this:

((ostringstream)(ostringstream("String") << 3)).str()

which is what I want to do (as I need to build the string on the fly.

What do I do?

I would prefer to use a template function to convert any type to a
string using a stringstream:

template <class StringType, class SourceType>
inline StringType string_cast(const SourceType& value)
{
std::basic_ostringstream<typename StringType::value_type> stream;
stream << value;
return stream.str();
}

Using it:
"String" + string_cast<std::string>(123)

Or using boost:
"String" + boost::lexical_cast<std::string>(123)

Hope this helps.

- --
Greetings, Thomas Jakob
quicix (at) gmail (dot) com
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.2 (MingW32)

iD8DBQFDunV7KzGu1S9H/nsRAms7AKC5JUXj9SNn6T1NNOia7H4gqVOWLACgtoea
hkFrjX7TsuZT39LNxs+DirY=
=RYlx
-----END PGP SIGNATURE-----
Jan 3 '06 #2
<an**************@googlemail.com> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com
I have a base class that takes a string to its constructor. I want to
construct a concrete class that does this:

class concrete: public base
{
public:
concrete() : base( /* create a stringstream here and convert to
string */ ){}
};

However, I can't get it to work. I can pass this:

ostringstream("String").str()

but not this:

(ostringstream("String") << 3).str()

or this:

((ostringstream)(ostringstream("String") << 3)).str()

which is what I want to do (as I need to build the string on the fly.

What do I do?


You need to cast to a reference to ostringstream, not to an ostringstream
object:

((ostringstream&)(ostringstream("String")<<3)).str ()

It would also be nicer if you used a static_cast:

static_cast<ostringstream&>(ostringstream("String" )<<3).str()
--
John Carson
Jan 3 '06 #3
>static_cast<ostringstream&>(ostringstream("String ")<<3).str()

To make this work I have added seekp:
(static_cast<ostringstream&>((ostringstream("Strin g").seekp(0,ios::end)
<< 3))).str()

Jan 3 '06 #4
"Dervish" <DA*******@yandex.ru> wrote in message
news:11**********************@g44g2000cwa.googlegr oups.com
static_cast<ostringstream&>(ostringstream("String" )<<3).str()


To make this work I have added seekp:
(static_cast<ostringstream&>((ostringstream("Strin g").seekp(0,ios::end)
<< 3))).str()


What "works" depends on what the objective is, on which the OP was not
explicit. If the objective is to add a 3 to the end of "String", then you
are right. You can, incidentally, make do with less brackets:

static_cast<ostringstream&>(ostringstream("String" ).seekp(0,ios::end)<<
3).str()
--
John Carson
Jan 3 '06 #5

John Carson wrote:
"Dervish" <DA*******@yandex.ru> wrote in message
news:11**********************@g44g2000cwa.googlegr oups.com
static_cast<ostringstream&>(ostringstream("String" )<<3).str()


To make this work I have added seekp:
(static_cast<ostringstream&>((ostringstream("Strin g").seekp(0,ios::end)
<< 3))).str()


What "works" depends on what the objective is, on which the OP was not
explicit. If the objective is to add a 3 to the end of "String", then you
are right. You can, incidentally, make do with less brackets:

static_cast<ostringstream&>(ostringstream("String" ).seekp(0,ios::end)<<
3).str()


Do not cast away what the compiler tells you is not allowed. The
compiler is telling you that for a reason.

ostringstream("String") creates a temporary. Now you can call str() on
it because that is a const function. But you cannot call << 3 because
that is a non-const function - it modifies the temporary. And that is
undefined behaviour.

No need for a one-liner. Use an inline function. One was supplied a few
posts above.

Jan 3 '06 #6
"Earl Purple" <ea********@gmail.com> wrote in message
news:11**********************@f14g2000cwb.googlegr oups.com
John Carson wrote:
"Dervish" <DA*******@yandex.ru> wrote in message
news:11**********************@g44g2000cwa.googlegr oups.com
static_cast<ostringstream&>(ostringstream("String" )<<3).str()

To make this work I have added seekp:
(static_cast<ostringstream&>((ostringstream("Strin g").seekp(0,ios::end)
<< 3))).str()


What "works" depends on what the objective is, on which the OP was
not explicit. If the objective is to add a 3 to the end of "String",
then you are right. You can, incidentally, make do with less
brackets:

static_cast<ostringstream&>(ostringstream("String" ).seekp(0,ios::end)<<
3).str()


Do not cast away what the compiler tells you is not allowed. The
compiler is telling you that for a reason.

ostringstream("String") creates a temporary. Now you can call str() on
it because that is a const function. But you cannot call << 3 because
that is a non-const function - it modifies the temporary. And that is
undefined behaviour.


The reason for the non-compilation is not because a temporary has been
modified. Modifying a temporary is perfectly legal. You may note that the
following argument compiles without warnings:

string("String").append(" extra bit")

The compilation fails because operator<< is inherited from ostringstream's
base class of basic_ostream<char> and thus operator<< returns a reference to
this base class of basic_ostream<char> rather than a reference to
ostringstream. basic_ostream<char> has no str() member function and that is
the error message given --- nothing about modifying a temporary. The cast is
a downcast, made with knowledge that the object referenced is indeed an
ostringstream and not merely a basic_ostream<char>.

--
John Carson
Jan 4 '06 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Bill Beacom | last post by:
Hi all, I am trying to use stringstream to assemble a text message from a set of user-defined objects that have the insertion operator overloaded. It seems to be putting them into the...
2
by: Woodster | last post by:
I am using std::stringstream to format a string. How can I clear the stringstream variable I am using to "re use" the same variable? Eg: Using std::string std::string buffer; buffer =...
3
by: Mike Chirico | last post by:
Sorry about the newbie question. What's the best way to convert numbers to strings in C++. The following works, but is it better than using the sprintf() "old C" way of converting? #include...
1
by: KidLogik | last post by:
Hello! I am using std::stringstream && std::string to parse a text file -> std::ifstream in; std::string s; std::streamstring ss; int n; I grab each line like so -> std::getline(in,s);
5
by: cherico | last post by:
I'd like to read stings from cin and put them in stringstream. I use a string object as an intermediate "container" to store data from cin and then put them in stringstream. stringstream ss ;...
19
by: Kai-Uwe Bux | last post by:
Hi folks, I have trouble writing a class, derving from stringstream, that collects item and once it's done will write them to std::cout in one go. It works fine except when I use it as a...
5
by: William Payne | last post by:
How do you get rid the contents in a std::stringstream? I'm using one in a for loop to format some status messages which I print to the screen. The stringstream member function clear() only clears...
9
by: martinezfive | last post by:
Hi, I feel no doubt some documentation contains my answer, so bare with me. Given the following: #inclde <stdio.h> #include <sstream> void f() { std::stringstream a("Hello World!\n");
2
by: david.crow | last post by:
Given the following input file: bob 1 2 3 4 5 mary 2 3 4 5 6 7 susan 3 4 5 6 7 8 9 This code snippet does not read it correctly: class Student {
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.