Boost logo

Boost :

From: Mathias Gaunard (mathias.gaunard_at_[hidden])
Date: 2007-12-15 22:26:09


Márcio Gil wrote:

> I suggest this add on lexical_cast library: a lexical_cast_def function,

> string source;
> int x = lexical_cast_def<int>( source, 0 );
>
> In opposite of:
>
> string source;
> int x;
> try
> {
> x = lexical_cast<int>( source );
> }
> catch(...)
> {
> x = 0;
> }

This wouldn't only be useful to lexical_cast.
What about a more generic utility?

template<typename F, typename T>
T call_default(F f, T t)
{
     try
     {
         return f();
     }
     catch
     {
         return t;
     }
}

Unfortunately, this requires usage of function objects, and lambdas are
not so popular.
Maybe one can do it with actual C++ expressions using macros.


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