Boost logo

Boost Users :

Subject: Re: [Boost-users] Conversion from numeric type to const char*
From: Roman Perepelitsa (roman.perepelitsa_at_[hidden])
Date: 2012-01-30 08:55:31


2012/1/30 Claude <clros_at_[hidden]>

> What is the better way for convert a numeric type into a const char *?
> I use:
>
> (boost::lexical_cast<std::string>(myNumericVar)).c_str()
>
> It is a good idea?

Depends how you use the value.

  // Safe.
  puts(boost::lexical_cast<std::string>(myNumericVar).c_str());

  const char* s = boost::lexical_cast<std::string>(myNumericVar).c_str();
  // Don't do this! Here 's' is a dangling pointer; the string has already
  // been destroyed.
  puts(s);

Roman Perepelitsa.



Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net