|
Boost Users : |
From: Mostafa (mostafa_working_away_at_[hidden])
Date: 2008-07-06 19:21:08
Helllo,
I was wondering why put_get_helper shouldn't be three structs (put_helper,
get_helper, put_get_helper, with the latter deriving from from the formers)
instead of one? My rationale is outlined below along with the proposed code.
//Begin Rationale
Problem: want a helper method for readable property maps.
Solution1: use exist boost::put_get_helper and just never use the put
function.
Problem with solution 1:
1) the put function will still be "callable", and
2) calling the put function will result in an invalid assignment to a
read-only variable compiler error, which is cryptic to decipher, ie, that
even though the put function does exist it should not be called. The
only way to clarify this would be via documentation, which then brings
about the problem of keeping code in sync with comments.
Solution2: make a helper method for readable property maps
that won't make the put companion function a valid function call,
ie, break up boost::put_get_helper to put_helper, get_helper, and
put_get_helper which derives from the latter two.
Problems with solution 2: None that I can see.
Advantages of solution 2:
1) It's self-documenting whether the put or get functions are intended
to be called, ie if I derive from get_helper then it's self-evident
that I shouldn't be calling the put function (vice versa); and
2) given the above scenario, even if by chance the put function get's called
the compiler error message will be easier to decipher, ie, something along the
lines of: <no matching function for call to âput(...)'>.
3) Not really an advantage, but ... backward compatibility is maintained.
//End Rationale.
//Proposed code adapted from boost/property_map.hpp
// A helper class for constructing a property map
// from a class that implements operator[]
template <class RetVal, class RvaluePropertyMap>
struct get_helper { };
template <class LvaluePropertyMap>
struct put_helper { };
template <class RetVal, class LvaluePropertyMap>
struct put_get_helper :
public put_helper<LvaluePropertyMap>,
public get_helper<RetVal, LvaluePropertyMap>
{ };
template <class PropertyMap, class RetVal, class K>
inline RetVal
get(
get_helper<RetVal, PropertyMap> const & pa,
K const & k)
{
RetVal v = static_cast<const PropertyMap&>(pa)[k];
return v;
}
template <class PropertyMap, class K, class V>
inline void
put(
put_helper<PropertyMap> const & pa,
K k,
V const & v)
{
static_cast<const PropertyMap&>(pa)[k] = v;
}
Thanks in advance,
-Mostafa
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