adding a bool to a string

A

aaragon

Hi All,

I'm trying to print boolean values and after I searched the entire web,
I couldn't find anything that does this =/ The code that I ended up
using is as follows:

string boolString(Object* obj_, size_t maxOutput_){
string string_;

for (size_t i=0; i<maxOutput_; i++){

if( sizeof(obj_->boolean(i)) == sizeof(bool) ){
if (obj_->boolean(i) == true)
string_ += "1";
else
string_ += "0";
}
}

return string_;
}

Is there any way to simplify this code?
 
R

Rolf Magnus

aaragon said:
Hi All,

I'm trying to print boolean values and after I searched the entire web,
I couldn't find anything that does this =/ The code that I ended up using
is as follows:

string boolString(Object* obj_, size_t maxOutput_){
string string_;

for (size_t i=0; i<maxOutput_; i++){

if( sizeof(obj_->boolean(i)) == sizeof(bool) ){

What is the above line supposed to do? What does obj_->boolean(i) return?
Note that sizeof is always computed at compile time.
if (obj_->boolean(i) == true)
string_ += "1";
else
string_ += "0";
}
}

return string_;
}

Is there any way to simplify this code?

string boolString(Object* obj_, size_t maxOutput_){
stringstream stream_;

for (size_t i=0; i<maxOutput_; i++)
// depending on the return type of boolean(), you can remove the cast
stream_ << bool(obj_->boolean(i));

return stream_.str();
}
 
V

Victor Bazarov

aaragon said:
I'm trying to print boolean values and after I searched the entire
web, I couldn't find anything that does this =/ The code that I
ended up using is as follows:

string boolString(Object* obj_, size_t maxOutput_){
string string_;

for (size_t i=0; i<maxOutput_; i++){

if( sizeof(obj_->boolean(i)) == sizeof(bool) ){

Why do you need to check the size? What does it give you?
if (obj_->boolean(i) == true)
string_ += "1";
else
string_ += "0";

This could be written

string_ += (obj_->boolean(i) ? '1' : '0');

which I find a bit simpler.

Also, consider that you call 'boolean' member twice here (once to
check the size and the other to actually get the value); if the
function has side effects, calling it twice may not be what you
want.
}
}

return string_;
}

Is there any way to simplify this code?

V
 
A

aaragon

Victor said:
Why do you need to check the size? What does it give you?

I need to check the size because the function may not return a bool but
an integer for example.
 
A

aaragon

Rolf said:
What is the above line supposed to do? What does obj_->boolean(i) return?
Note that sizeof is always computed at compile time.


string boolString(Object* obj_, size_t maxOutput_){
stringstream stream_;

for (size_t i=0; i<maxOutput_; i++)
// depending on the return type of boolean(), you can remove the cast
stream_ << bool(obj_->boolean(i));

return stream_.str();
}

Sometimes boolean() may return an integer, so doing a cast to a boolean
is not a good idea. This is the reason behind the check of the size of
whatever type boolean() returns. I thought that the sream can
automatically convert to 1 or 0 from a boolean as

stream_ += boolean(i);

but this is not the case. However, if I repeat the previous with an
integer, it works fine.
 
V

Victor Bazarov

Rolf said:
What is the above line supposed to do? What does obj_->boolean(i)
return? Note that sizeof is always computed at compile time.


string boolString(Object* obj_, size_t maxOutput_){
stringstream stream_;

for (size_t i=0; i<maxOutput_; i++)
// depending on the return type of boolean(), you can remove the
cast stream_ << bool(obj_->boolean(i));

return stream_.str();
}

Consider that some locales do not have '1' or '0' as the default
for outputting a bool value. If the OP wanted '1' or '0', it should
probably be explicit.

And how is using a stringstream simpler (better) than, say,

for (size_t i=0; i<maxOutput_; i++)
string_ += (bool(obj_->boolean(i)) ? '1' : '0');

return string_;

?

V
 
H

Howard

aaragon said:
I need to check the size because the function may not return a bool but
an integer for example.

And how do you know that a value is a bool just because it's the same size
as one?

Also, boolean() is your own function, so you should already know what its
return type is. It can't sometimes return a bool and other times return an
int. (Unless, I guess, if "boolean" isn't really a function member of obj_
at all, but is instead a function pointer which you assign elsewhere.)

-Howard
 
V

Victor Bazarov

aaragon said:
I need to check the size because the function may not return a bool
but an integer for example.

So? An integer is convertible to bool. Besides, sizes of 'bool' and
'int' _can_ be the same. The check is no good. You need to check
'typeid' to be sure.

V
 
A

aaragon

Consider that some locales do not have '1' or '0' as the default
for outputting a bool value. If the OP wanted '1' or '0', it should
probably be explicit.

And how is using a stringstream simpler (better) than, say,

for (size_t i=0; i<maxOutput_; i++)
string_ += (bool(obj_->boolean(i)) ? '1' : '0');

return string_;

?

Well, this definitely will work for booleans. But, the object I'm
working on, would also be a container for other values (say double,
integers) in which case boolean representation is no longer useful
(forget about the name boolean(i), it could be anything, like
value(i)). Therefore, the cast that you're using is not useful
anymore.
 
V

Victor Bazarov

Howard said:
And how do you know that a value is a bool just because it's the same
size as one?

Also, boolean() is your own function, so you should already know what
its return type is. It can't sometimes return a bool and other times
return an int. (Unless, I guess, if "boolean" isn't really a
function member of obj_ at all, but is instead a function pointer
which you assign elsewhere.)

If it's a function pointer, the return value type is part of the type
of that pointer. C++ is strict in that regard. You cannot have return
value types change arbitrarily during run-time.

V
 
V

Victor Bazarov

aaragon said:
Well, this definitely will work for booleans. But, the object I'm
working on, would also be a container for other values (say double,
integers) in which case boolean representation is no longer useful
(forget about the name boolean(i), it could be anything, like
value(i)). Therefore, the cast that you're using is not useful
anymore.

If for some twisted reason the 'boolean' function returns an 'int'
and not a 'bool', it shouldn't be used here, which means you need
to employ some kind of templated mechanism for rejecting classes
with 'boolean' members returning anything except 'bool'. See your
favourite C++ book on how to specialise your function on that.

V
 
H

Howard

If it's a function pointer, the return value type is part of the type
of that pointer. C++ is strict in that regard. You cannot have return
value types change arbitrarily during run-time.

Oh, right, of course. Thanks.

-Howard
 
R

Rolf Magnus

aaragon said:
Sometimes boolean() may return an integer, so doing a cast to a boolean
is not a good idea.

Could you elaborate on how you think you did that? It's not possible. A
function's return type is fixed at compile time. Somewhere, you declared
the function, and part of that declaration is the return type. So if
boolean() returns a bool, it will always do that.
This is the reason behind the check of the size of whatever type boolean()
returns.

The size of bool depends on the implementation. Most often, it's the same as
char or the same as int.
I thought that the sream can automatically convert to 1 or 0 from a
boolean as

stream_ += boolean(i);

You need operator <<, not += to put something into a stream.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,901
Latest member
Noble71S45

Latest Threads

Top