czchlong
czchlong

Reputation: 2584

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.

Upvotes: 1

Views: 5987

Answers (1)

Mike Seymour
Mike Seymour

Reputation: 254481

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.

Upvotes: 4

Related Questions