Initialization of an unsigned int by a negative number (-1 here which would be std::numeric_limits<unsigned int>::max()) is allowed by the standards. The integral conversion works alright. An example from the standards itself is std::string::npos. So, I think it is okay that an exception isn’t thrown.

 

Thanks and regards,

Abhishek Padmanabh

 

From: boost-users-bounces@lists.boost.org [mailto:boost-users-bounces@lists.boost.org] On Behalf Of Kris Wragg
Sent: 26 February 2008 21:18
To: boost-users@lists.boost.org
Subject: [Boost-users] lexical_cast not throwing exception when converting negative value to UINT

 

Dear subscribers,

 

I am using lexical_cast to cast a string to an UINT, unfortunately, it is not throwing an exception if a value is negative.

 

Example:

 

std::wstring failString = L”-1”;

UINT nTest = boost::lexical_cast<UINT>(failString);

 

This does not throw a bad_lexical_cast exception, which it should, as -1 is not a valid unsigned integer.

 

Perhaps I am using the wrong function, or using this one incorrectly, I’ve only just started using boost after moving to a new project that’s not based on ancient legacy code!

 

Regards,

Kris Wragg

 


 

This email and all attachments transmitted with it contain confidential information belonging to the sender. It is intended only for the addressee(s) stated above. Should you not be the intended recipient then any disclosure, copying, distribution or the taking of any action in reliance on the contents of this email is strictly prohibited. If you have received this transmission in error, please notify the sender immediately and permanently delete this message and destroy any copies.

 

Internet communications cannot be guaranteed to be timely, secure, error-free or virus-free and Sender expressly does not warrant or guarantee that this e-mail is virus free.  Recipient is advised to protect itself by taking the necessary steps to ensure that appropriate security measures are in place and to utilize a virus scanner before opening any attachment or down loading any information or documentation using links or other contents in this e-mail.

 

You may opt out of receiving future communications from us by replying to this e-mail and replacing the subject line with UNSUBSCRIBE.