|
Boost : |
Subject: [boost] [convert] are you mixing default_value and error_value?
From: vicente.botet (vicente.botet_at_[hidden])
Date: 2009-07-06 02:20:57
Hi,
I have read the documentation and I think that you are mixing default value with error value. The fact that a type is not DefaultConstructible do not implies that the user wants to have an exception when the conversion fails. This are two orthogonal dimensions.
I think that for these non Default constructible types you can use a trait that gives you a default value for types not having a default constructor. E.g.
template <typename T>
struct default_value_trait {
static const T value=T();
};
template <>
struct default_value_trait<direction> {
static const direction value=direction::up;
};
So now you don't need any more to pass the default value as parameter, and the following should compile
direction dir = convert<direction>::from (str);
and could throw if conversion fails after minor modification on the implementation.
I suppose you can reach the same effect with the error_value needed when the user want no_thow semantics.
template <typename T>
struct error_value_trait {
static const T value=T();
};
template <>
struct error_value_trait<direction> {
static const direction value=direction::down;
};
So the following
direction dir = convert<direction>::from (str)(_thows=false);
will return direction::down when conversion fails after some modification on the implementation
One more question raise now. Should the fact the conversion throws or not when failing be part of the function type or a function parameter?
Do we need
direction dir = convert<direction, no_thows<> >::from (str);
or
direction dir = convert<direction>::from (str)(_thows=false);
Resumein, I think these modifications are important, so the convert function can have always only the From parameter, and the throw semantics is preserved for non DefaultConstructible types.
HTH,
Vicente
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk