S
Steven C.
string strS;
stringstream stmT;
double dR;
stmT << strS;
stmT >> dR;
Is this the best way?
stringstream stmT;
double dR;
stmT << strS;
stmT >> dR;
Is this the best way?
Steven said:string strS;
stringstream stmT;
double dR;
stmT << strS;
stmT >> dR;
Is this the best way?
Ryan Winter said:Thats not bad. I think this would be better though:
#include <sstream>
#include <string>
#include <ostream>
template <typename T>
bool fromString(const std::string &s, T &result)
{
std::istringstream stream ss;
return (stream >> result)
}
int main()
{
double d(0.0);
std::string s("34.543");
if (!fromString(s, d))
{
std::cerr << "error" << std::endl;
}
}
It doesnt need exceptions, and has greater type safety as you dont
need to specify the template type.
#include said:[snip]#include <string>
#include <ostream>
template <typename T>
bool fromString(const std::string &s, T &result)
{
std::istringstream stream ss;
return (stream >> result)
}
int main()
{
double d(0.0);
std::string s("34.543");
if (!fromString(s, d))
{
std::cerr << "error" << std::endl;
}
}
The other solution doesn't "need" exceptions as well
- BTW, writing
an error to stderr from a function designed to be put in a library
is not very sensible IMHO - what happens if you use it from a GUI
program?
Apart from that: the original solution is IMHO superior in that
you can do sth like
double d = fromString<double>(s);
whereas with your solution I'd be forced to write
double d;
fromString(s,d);
And why should you gain greater type safety if you can omit the
template type???
Buster Copley said:No, you should test the stream state after reads and writes.
If you're going to do a lot of conversions from strings,
[snip]
double dR = from_string <double> (strS);
Regards,
Buster.
Buster said:#include <sstream>
#include <string>
#include <ostream>
template <typename T>
bool fromString(const std::string &s, T &result)
{
std::istringstream stream ss;
return (stream >> result)
}
int main()
{
double d(0.0);
std::string s("34.543");
if (!fromString(s, d))
{
std::cerr << "error" << std::endl;
}
}
[snip]
The other solution doesn't "need" exceptions as well
Erm. Yes it does.
- BTW, writing
an error to stderr from a function designed to be put in a library
is not very sensible IMHO - what happens if you use it from a GUI
program?
This hypothetical library would contain 'fromString', not 'main'.
Apart from that: the original solution is IMHO superior in that you
can do sth like
double d = fromString<double>(s);
Thank you very much, but I don't agree.
whereas with your solution I'd be forced to write
double d;
fromString(s,d);
Careful now. You forgot the error checking:
if (fromString (s, d))
{
// non-exceptional code here
}
else
{
// other non-exceptional code. don't use d!
}
And why should you gain greater type safety if you can omit the
template type???
char c = from_string <int> ("1000000"); // oops
Buster Copley said:[snip]#include <sstream>
#include <string>
#include <ostream>
template <typename T>
bool fromString(const std::string &s, T &result)
{
std::istringstream stream ss;
return (stream >> result)
}
int main()
{
double d(0.0);
std::string s("34.543");
if (!fromString(s, d))
{
std::cerr << "error" << std::endl;
}
}
The other solution doesn't "need" exceptions as well
Erm. Yes it does.
- BTW, writing
an error to stderr from a function designed to be put in a library
is not very sensible IMHO - what happens if you use it from a GUI
program?
This hypothetical library would contain 'fromString', not 'main'.
Thank you very much, but I don't agree.
Careful now. You forgot the error checking:
if (fromString (s, d))
{
// non-exceptional code here
}
else
{
// other non-exceptional code. don't use d!
}
Thanks - this shows exactly why exceptions are preferable to error
codes. When using error codes, I have to clutter every function
in the call stack with error checking statements, whereas with
exceptions I can handle the error *once and for all* where I
want to.
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.