views:

5547

answers:

14

Maybe this is a dumb question, but is there any way to convert a boolean value to a string such that 1 turns to "true" and 0 turns to "false"? I could just use an if statement, but it would be nice to know if there is a way to do that with the language or standard libraries. Plus, I'm a pedant. :)

A: 

I usually write myself a helper static function to do that. Or you could use a macro too. I don't think there is no language construct for that.

A: 

I agree that a macro might be the best fit. I just whipped up a test case (believe me I'm no good with C/C++ but this sounded fun):

#include <stdio.h>
#include <stdarg.h>

#define BOOL_STR(b) (b?"true":"false")

int main (int argc, char const *argv[]) {
    bool alpha = true;
    printf( BOOL_STR(alpha) );
    return 0;
}
Joseph Pecoraro
A: 

Try this Macro. Anywhere you want the "true" or false to show up just replace it with PRINTBOOL(var) where var is the bool you want the text for.

#define PRINTBOOL(x) x?"true":"false"
dagorym
Need some parentheses in that macro, which is probably why you got the downvote.
postfuturist
A: 

Thanks guys. I hadn't thought of using the ternary.

Jason Baker
A: 

I use a ternary in a printf like this: printf("%s\n", b?"true":"false");

If you macro it : B2S(b) ((b)?"true":"false")

then you need to make sure whatever you pass in as 'b' doesn't have any side effects. And don't forget the brackets around the 'b' as you could get compile errors.

As 'b' only shows up once in the macro definition, why are you warning of side effects?
postfuturist
+2  A: 

If you decide to use macros (or are using C on a future project) you should add parenthesis around the 'b' in the macro expansion (I don't have enough points yet to edit other people's content):

#define BOOL_STR(b) ((b)?"true":"false")

This is a defensive programming technique that protects against hidden order-of-operations errors; i.e., how does this evaluate for all compilers?

1 == 2 ? "true" : "false"

compared to

(1 == 2) ? "true" : "false"
dwj
+21  A: 

How about using the C++ language itself?

bool t = true;
bool f = false;
std::cout << std::noboolalpha << t << " == " << std::boolalpha << t << std::endl;        
std::cout << std::noboolalpha << f << " == " << std::boolalpha << f << std::endl;
graham.reeds
+7  A: 

We're talking about C++ right? Why on earth are we still using macros!?

C++ inline functions give you the same speed as a macro, with the added benefit of type-safety and parameter evaluation (which avoids the issue that Rodney and dwj mentioned.

inline const char * const BoolToString(bool b)
{
  return b ? "true" : "false";
}

Aside from that I have a few other gripes, particularly with the accepted answer :)

// this is used in C, not C++. if you want to use printf, instead include <cstdio>
//#include <stdio.h>
// instead you should use the iostream libs
#include <iostream>

// not only is this a C include, it's totally unnecessary!
//#include <stdarg.h>

// Macros - not type-safe, has side-effects. Use inline functions instead
//#define BOOL_STR(b) (b?"true":"false")
inline const char * const BoolToString(bool b)
{
  return b ? "true" : "false";
}

int main (int argc, char const *argv[]) {
    bool alpha = true;

    // printf? that's C, not C++
    //printf( BOOL_STR(alpha) );
    // use the iostream functionality
    std::cout << BoolToString(alpha);
    return 0;
}

Cheers :)


At Dr Pizza: Include a whole boost lib for the sake of a function this simple? You've got to be kidding?

OJ
+10  A: 

Jesus wept.

OK, you asked about C++. Not C. Not frigging macros. C++.

C++ has proper strings. Not C's stupid NTBS nonsense. Proper strings. Use them! They're in the standard header string. #include <string> to use them. No more strcat/strcpy buffer overruns; no more missing null terminators; no more messy manual memory management; proper counted strings with proper value semantics.

C++ has the ability to convert bools into human-readable representations too. We saw hints at it earlier with the iostream examples, but they're a bit limited because they can only blast the text to the console (or with fstreams, a file). Fortunately, the designers of C++ weren't complete idiots; we also have iostreams that are backed not by the console or a file, but by an automatically managed string buffer. They're called stringstreams. #include <sstream> to get them. Then we can say:

std::string bool_as_text(bool b)
{
    std::stringstream converter;
    converter << b;
    return converter.str();
}

Of course, we don't really want to type all that. Fortunately, C++ also has a convenient third-party library named Boost that can help us out here. Boost has a nice function called lexical_cast. We can use it thus:

boost::lexical_cast(my_bool)

Now, it's true to say that this is higher overhead than some macro; stringstreams deal with locales which you might not care about, and create a dynamic string (with memory allocation) whereas the macro can yield a literal string, which avoids that. But on the flip side, the stringstream method can be used for a great many conversions between printable and internal representations. You can run 'em backwards; boost::lexical_cast<bool>("true") does the right thing, for example. You can use them with numbers and in fact any type with the right formatted I/O operators. So they're quite versatile and useful.

And if after all this your profiling and benchmarking reveals that the lexical_casts are an unacceptable bottleneck, that's when you should consider doing some macro horror.

DrPizza
A: 

The accepted answer really has several drawbacks, most of which have been mentioned here. Is there a reason not to use the canonical C++ way described by Graham?

/EDIT: Is now obsolete.

Konrad Rudolph
A: 

At Dr Pizza: Include a whole boost lib for the sake of a function this simple? You've got to be kidding

It's a single header from the library, and there's sure to be other useful stuff in boost anyway. So, no, I'm not kidding.

DrPizza
A: 

This should be fine:


inline const char* bool_cast(const bool b) {
    return b ? "true" : "false";
}

But, if you want to do it more C++-ish:


#include <iostream>
#include <string>
#include <sstream>
using namespace std;

string bool_cast(const bool b) {
    ostringstream ss;
    ss << boolalpha << b;
    return ss.str();
}

int main() {
    cout << bool_cast(true) << "\n";
    cout << bool_cast(false) << "\n";
}
Shadow2531
A: 

As long as strings can be viewed directly as a char array it's going to be really hard to convince me that std::string represents strings as first class citizens in C++.

Besides, combining allocation and boundedness seems to be a bad idea to me anyways.

MSN

Mat Noguchi
A: 

Besides, combining allocation and boundedness seems to be a bad idea to me anyways.

I don't understand what you mean by this at all. All allocations are necessarily bounded (I have a finite address space), so they have no choice but to be combined.

DrPizza