boost::lexical_castbool

Started by
5 comments, last by Fruny 18 years ago
I'm having a little bit of trouble with boost::lexical_cast<bool> and and c-style strings.

char* s = "true";

if( boost::lexical_cast<bool>(s) )
	printf("\n True!! \n");
else
	printf("\n False!! \n");
Even with something this simple, I keep getting the boost::bad_lexical_cast exception thrown.
Quote:From http://lists.boost.org/Archives/boost/2002/05/30088.php If we allow implicit conversion (the seconds step), then a careful study of the conversion rules in C++ is needed, to make sure it does the right thing. For example, there's an implicit conversion from a pointer, to bool, so "bool b=lexical_cast<bool>("false")" (when boolalpha is enabled) will always give true (because the char * pointer is non-null). This needs to be checked for, and handled, at the "special case" step. That's why that step has to be first.
Although this isn't exactly my problem, since I'm using a char* string and attempting to convert to bool, I was wondering if this could be the problem. Could it be that because I'm attempting to convert to a boolean, and that it's a char pointer?
[size="2"][size=2]Mort, Duke of Sto Helit: NON TIMETIS MESSOR -- Don't Fear The Reaper
Advertisement
You're looking at a discussion. Not documentation that describes how it actually acts.
I realise that, but I'm grapsing at straws. I'm using lexical_cast for booleans the same way that I'm using it for doubles, floats and integers, and it's not working. It only throws an exception for the boolean cast, and I haven't yet been able to find something in the boost documentation that might hint at what the problem is.
[size="2"][size=2]Mort, Duke of Sto Helit: NON TIMETIS MESSOR -- Don't Fear The Reaper
Is there a specific reason you need to cast a char* to a boolean? Why not simply strcmp() them, it'll prevent needing an additional library, it's a standard function, and it's very simple.

char* s = "true";if(strcmp(s,"true") == 0)    printf("\n True!! \n");else    printf("\n False!! \n");
I definately could, but since I'm using boost for everything else in that area of my engine, I was wondering if there was a quick and easy fix to get it working.

I could use strcmp, but when using boost::lexical_cast to convert to a boolean, does is it case sensetive? Because I'd have to do a tiny bit of fiddling if I used strcmp.
[size="2"][size=2]Mort, Duke of Sto Helit: NON TIMETIS MESSOR -- Don't Fear The Reaper
Where are you getting the strings "true" and "false" from? I think you'd have better luck converting from "0" and "1"
Quote:Original post by RDragon1
Where are you getting the strings "true" and "false" from? I think you'd have better luck converting from "0" and "1"


std::boolalpha enables input and output of boolean values in text form. The exact strings being used depend on the stream's imbued locale.
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan

This topic is closed to new replies.

Advertisement