Boost logo

Boost :

From: Marcin Kalicinski (kalita_at_[hidden])
Date: 2006-05-08 09:20:27


> I don't really se the need for the converting get functions.
>
> Why is pt.get<double>(path) better than
> lexical_cast<double>(pt.get(path)).
> (or string_to or atof)

There will be problems with non-throwing failure handling. Users will be
forced to write something like that:

double d;
try
{
    d = boost::lexical_cast<double>(pt.get(path, default_value));
}
catch (boost::bad_lexical_cast &)
{
   d = default_value;
}

instead of just:

double d = pt.get(path, default_value);

Even if failure handling is not an issue, explicit use of lexical_cast
forces uses to do one extra include, and writing:

boost::lexical_cast<int>(pt.get("path")); // #1

instead of:

pt.get<int>(path); // #2

Which in my opinion is much less readable. 95% of uses will do type
conversion anyway (that comes from my personal experience with the library),
so forcing users to write #1 instead of #2 in all these cases will do no
good.

Also, user can use lexical_cast now if he really wants to:

int n = boost::lexical_cast<int>(pt.get<std::string>("path"));

Best regards,
Marcin


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk