|
Boost Users : |
Subject: Re: [Boost-users] [fusion] iterating over a sequence with non-const references
From: Joel de Guzman (joel_at_[hidden])
Date: 2012-04-02 23:26:28
On 4/3/2012 2:45 AM, paul Fultz wrote:
>
>
>> push_back is designed to be non-mutating. That is why. This is
>> true of all the views returned from 'algos', by design. What
>> you can do is to make a container from the view through, e.g.
>> as_vector, as_list. It is *not* true that all sequences return
>> const refs to its elements. Containers definitely return references,
>> otherwise get<N>(c)= expr will not be possible.
>
> Also, front and back return non-const reference, but thats not the point.
> During iteration, they return const reference. Thats why this is not
> possible:
>
> struct mutable_func
> {
> template<class F>
> struct result;
>
> template<class F, class T, class U>
> struct result<F(T, U)>
> {
> typedef T type;
> };
>
> template<class T, class U>
> T operator()(T && x, U y) const
> {
> return x+=y;
> }
> };
>
> int i = 0;
> invoke(foo, tuple<int&, int>(i, 1)); //doesn't compile
>
> here is the output:
What is 'foo'? Anyway, this (below) compiles fine with me with
g++ 4.6.1 and MSVC 10:
#include <boost/fusion/functional/invocation/invoke.hpp>
#include <boost/fusion/tuple.hpp>
#include <boost/fusion/sequence/io.hpp>
#include <iostream>
struct mutable_func
{
template<class F>
struct result;
template<class F, class T, class U>
struct result<F(T, U)>
{
typedef T type;
};
template<class T, class U>
T operator()(T && x, U y) const
{
return x+=y;
}
};
int main()
{
using namespace boost::fusion;
int i = 999;
std::cout << invoke(mutable_func(), tuple<int&, int>(i, 1)) << std::endl;
return 0;
}
Prints 1000. What am I missing?
Regards,
-- Joel de Guzman http://www.boostpro.com http://boost-spirit.com
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