Boost logo

Boost Users :

Subject: Re: [Boost-users] [Fusion] Output of adapted ADT
From: Joel de Guzman (joel_at_[hidden])
Date: 2011-04-09 00:07:09

On 4/8/2011 7:34 PM, Tore Halvorsen wrote:
> On Fri, Apr 8, 2011 at 11:53 AM, TONGARI<tongari95_at_[hidden]> wrote:
>>> I'm trying to output a string from an adapted class, but I'm getting
>>> 1>main.cpp(33): error C2679: binary '<<' : no operator found which
>>> takes a right-hand operand of type
>>> 'boost::fusion::extension::adt_attribute_proxy<foo,0,false>' (or there
>>> is no acceptable conversion)
>>> for the following code (boost 1_46_1, msvc2010)
>>> Is there a header I'm missing? Very similar code works for
>>> BOOST_FUSION_DEFINE_STRUCT - and using ints or doubles also work...
>> In that case, boost::fusion::front returns not the real element, but a
>> attribute_proxy that separates the use of getter& setter.
>> Here's a workaround:
>> std::cout<< (std::string const&)boost::fusion::front(f)<< std::endl;
> Thanks, but I was not really looking for a workaround. My original problem was
> actually trying to use an adapted ADT with spirit::karma, but thought I'd tackle
> this seemingly simpler issue first.
> (same as above)
> foo f("foo");
> using namespace boost::spirit::karma;
> generate(std::ostream_iterator<char>(std::cout), string<< eol, f);
> ... which fails with error C2039: 'const_iterator' : is not a member
> of 'boost::fusion::extension::adt_attribute_proxy<foo,0,true>'
> So, I guess that adapted_adts really can't be used in the same
> situations as adapted structs?

Adapted classes are different beasts. Adapted classes may not
expose their attributes by reference due to abstraction and
information hiding. It's not uncommon for a class not to actually
have an actual member variable. The best we can assume is that a
class has a getter and setter member functions. When you access
its member (e.g. using at_c or dereferencing an iterator), here's
what happens:

1) If the adapted class is immutable, it returns the class' adapted
    get member function for the member.

    E.g. T get_mem() const;

2) If the adapted class is mutable (your case), then it returns a
    proxy, that when assigned to, calls the class' adapted set member
    function for the member.

    E.g. void set_mem(T const&);

    The proxy also has a conversion to T which then returns the class'
    adapted get member function for the member.

That's the best we can do.


Joel de Guzman

Boost-users list run by williamkempf at, kalb at, bjorn.karlsson at, gregod at, wekempf at