Search code examples
c++boostlexical-cast

boost::lexical_cast from string to char exception


I am new to using boost::lexical_cast and have minimal understanding of its internals. I am trying to do the following cast:

string someString = boost::lexical_cast<char>(sourceString);

However, boost is complaining that the above code is:

[Exception]: bad lexical cast: source type value could not be interpreted as target

The source is a string, however it will always only be 1 character long.

Could someone please explain?

Thanks.


Solution

  • When I test it (after fixing the invalid conversion from char to string), the lexical cast succeeds as long as sourceString contains a single character. Here are the test results.

    If your real code doesn't work, then please post more of it; preferably a runnable program that demonstrates the error.