Boost logo

Boost Users :

Subject: Re: [Boost-users] [attn. Dave Abrahams] workaround ideas?
From: Ovanes Markarian (om_boost_at_[hidden])
Date: 2009-03-03 17:27:20


Noah,

please see my answer below:

On Tue, Mar 3, 2009 at 5:59 PM, Noah Roberts <roberts.noah_at_[hidden]> wrote:

>
> template < typename FIELD, typename CDR >
> typename FIELD::type & get(rec< FIELD, CDR > & r)
> {
> return r.value;
> }

 You should use the static_cast<SomeFieldSpecificType>(r) here to fix the
problem. The only problem is, that 'r' is the entire recordset, and you
can't cast it to itself, nor you can extract the SomeFieldSpecificType.

Your example work with MSVC 9. I have rewritten it to use the static_cast,
but did not test it with MSVC 8. Give it a try, I think this should work:

#include <boost/mpl/fold.hpp>
#include <boost/mpl/vector.hpp>
#include <boost/mpl/inherit.hpp>
#include <boost/mpl/identity.hpp>
#include <boost/mpl/placeholders.hpp>
#include <boost/mpl/inherit_linearly.hpp>

namespace pl = boost::mpl::placeholders;
namespace mpl = boost::mpl;

struct empty {};

template <typename FIELD>
struct rec
{
    typename FIELD::type value;

    rec() : value() {}
};

template < typename VEC >
struct generate_record
    : boost::mpl::inherit_linearly< VEC, mpl::inherit< rec<pl::_2>, pl::_1>
>::type
{};

template < typename FIELD, class Records>
typename FIELD::type & get(Records& rs)
{
    return static_cast<rec<FIELD>&>(rs).value;
}

typedef mpl::identity<int> field1;
typedef mpl::identity<double> field2;
typedef mpl::identity<char> field3;
typedef mpl::identity<int> field4;

typedef generate_record<mpl::vector< field1, field2, field3> >::type
test_record;

int main()
{
    test_record tr;
    get<field1>(tr) = 3;
    get<field2>(tr) = 3.0;
    get<field3>(tr) = 'a';
    get<field4>(tr) = 10; //this won't work as intended

    return 0;
}

This might be interesting to Dave:
If same types are passed into the inheritance, no compiler error is issued
since the types are probably managed by mpl::set(???). Compiler will
silently compile but get<field4>(tr) = 10 will overwrite the value of
get<field1>(tr), which is probably not intended. And we intend in MPL to
produce as mucht errors as possible, since the compiler is our tester ;)

I would expect, that at least here an error is produced, that I am trying to
derive from the same type twice. This is a philosofical discussion, since I
can modify inherit to inherit virtually and this might be fine than, but
throwing away the type from the inheritance is not ok. At least MSVC
produced the following type:

boost::mpl::inherit2<rec<boost::mpl::identity<char>
>,boost::mpl::inherit2<rec<boost::mpl::identity<double>
>,rec<boost::mpl::identity<int> > > >

With Kind Regards,
Ovanes



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