Converting an std::string to an int

Hi: I need to convert and std::string to an int, should I use Lexical_cast or can I just use static_cast? Sean. __________ Information from ESET NOD32 Antivirus, version of virus signature database 3196 (20080618) __________ The message was checked by ESET NOD32 Antivirus. http://www.eset.com

Hi Sean.
I need to convert and std::string to an int, should I use Lexical_cast or can I just use static_cast?
No way to use static_cast<> for this, but lexical_cast<> is ok. One thing I do not know about lexical_cast is whether it does any dynamic memory allocation internally which it should not need to do in this specific case. Such memory allocation can throw std::bad_alloc exceptions and may or may not be a performance bottleneck depending on your use-case. If this bothers you, you can always use atoi() or something similar. Hope this helps. Best regards, Jurko Gospodnetić

Hi: Cheers lexical_cast wor perfectly. Sean. -----Original Message----- From: boost-users-bounces@lists.boost.org [mailto:boost-users-bounces@lists.boost.org] On Behalf Of Jurko Gospodnetic Sent: 18 June 2008 18:08 To: boost-users@lists.boost.org Subject: Re: [Boost-users] Converting an std::string to an int Hi Sean.
I need to convert and std::string to an int, should I use Lexical_cast or can I just use static_cast?
No way to use static_cast<> for this, but lexical_cast<> is ok. One thing I do not know about lexical_cast is whether it does any dynamic memory allocation internally which it should not need to do in this specific case. Such memory allocation can throw std::bad_alloc exceptions and may or may not be a performance bottleneck depending on your use-case. If this bothers you, you can always use atoi() or something similar. Hope this helps. Best regards, Jurko Gospodnetić _______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users __________ Information from ESET NOD32 Antivirus, version of virus signature database 3198 (20080618) __________ The message was checked by ESET NOD32 Antivirus. http://www.eset.com __________ Information from ESET NOD32 Antivirus, version of virus signature database 3198 (20080618) __________ The message was checked by ESET NOD32 Antivirus. http://www.eset.com

Sean Farrow wrote:
I need to convert and std::string to an int, should I use Lexical_cast or can I just use static_cast?
You cannot use static_cast to convert an std::string to an int. But I
think that lexical_cast is indeed the right thing for you.
#include
participants (6)
-
Delfin Rojas
-
Jurko Gospodnetić
-
Nat Goodspeed
-
Niels Dekker - mail address until 2008-12-31
-
Sean Farrow
-
Steven Watanabe