Connecting Tech Pros Worldwide Forums | Help | Site Map

adding a bool to a string

aaragon
Guest
 
Posts: n/a
#1: Nov 15 '06
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?


Rolf Magnus
Guest
 
Posts: n/a
#2: Nov 15 '06

re: adding a bool to a string


aaragon wrote:
Quote:
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.
Quote:
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();
}

Victor Bazarov
Guest
 
Posts: n/a
#3: Nov 15 '06

re: adding a bool to a string


aaragon wrote:
Quote:
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?
Quote:
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.
Quote:
}
}
>
return string_;
}
>
Is there any way to simplify this code?
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask


aaragon
Guest
 
Posts: n/a
#4: Nov 15 '06

re: adding a bool to a string



Victor Bazarov wrote:
Quote:
aaragon wrote:
Quote:
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?
I need to check the size because the function may not return a bool but
an integer for example.
Quote:
>
Quote:
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.
>
Quote:
}
}

return string_;
}

Is there any way to simplify this code?
>
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
aaragon
Guest
 
Posts: n/a
#5: Nov 15 '06

re: adding a bool to a string



Rolf Magnus wrote:
Quote:
aaragon wrote:
>
Quote:
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.
>
Quote:
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();
}
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.

Victor Bazarov
Guest
 
Posts: n/a
#6: Nov 15 '06

re: adding a bool to a string


Rolf Magnus wrote:
Quote:
aaragon wrote:
>
Quote:
>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.
>
Quote:
> 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();
}
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
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask


Howard
Guest
 
Posts: n/a
#7: Nov 15 '06

re: adding a bool to a string



"aaragon" <alejandro.aragon@gmail.comwrote in message
news:1163617470.334923.67870@k70g2000cwa.googlegro ups.com...
Quote:
>
Victor Bazarov wrote:
Quote:
>aaragon wrote:
Quote:
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?
>
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



Victor Bazarov
Guest
 
Posts: n/a
#8: Nov 15 '06

re: adding a bool to a string


aaragon wrote:
Quote:
Victor Bazarov wrote:
Quote:
>aaragon wrote:
Quote:
>>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?
>
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.
Quote:
[..]
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask


aaragon
Guest
 
Posts: n/a
#9: Nov 15 '06

re: adding a bool to a string


>
Quote:
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.
Quote:
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Victor Bazarov
Guest
 
Posts: n/a
#10: Nov 15 '06

re: adding a bool to a string


Howard wrote:
Quote:
"aaragon" <alejandro.aragon@gmail.comwrote in message
news:1163617470.334923.67870@k70g2000cwa.googlegro ups.com...
Quote:
>>
>Victor Bazarov wrote:
Quote:
>>aaragon wrote:
>>>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?
>>
>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.)
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
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask


Victor Bazarov
Guest
 
Posts: n/a
#11: Nov 15 '06

re: adding a bool to a string


aaragon wrote:
Quote:
Quote:
>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.
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
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask


Howard
Guest
 
Posts: n/a
#12: Nov 15 '06

re: adding a bool to a string



"Victor Bazarov" <v.Abazarov@comAcast.netwrote in message
news:ejfp4p$hhq$1@news.datemas.de...
Quote:
Quote:
>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.
>
Oh, right, of course. Thanks.

-Howard


Rolf Magnus
Guest
 
Posts: n/a
#13: Nov 15 '06

re: adding a bool to a string


aaragon wrote:
Quote:
Quote:
>What is the above line supposed to do? What does obj_->boolean(i) return?
>Note that sizeof is always computed at compile time.
>>
Quote:
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();
>}
>
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.
Quote:
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.
Quote:
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.

Closed Thread