[Range, Phoenix] What am I missing...

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

[Range, Phoenix] What am I missing...

Robert Jones-2
Hi Folks

Can anyone explain the output of this program...

#include <iostream>
#include <boost/range/algorithm/copy.hpp>
#include <boost/range/counting_range.hpp>
#include <boost/range/adaptor/reversed.hpp> 
#include <boost/range/adaptor/transformed.hpp> 
#include <boost/phoenix/bind/bind_function.hpp>
#include <boost/phoenix/core/argument.hpp>


int f( int i ) { return i * 2; }

int main( )
{
    using boost::adaptors::reversed;
    using boost::adaptors::transformed;
    using boost::counting_range;
    using boost::copy;
    using boost::phoenix::arg_names::_1;

    copy( counting_range( 1, 5 ) | reversed | transformed( f ),
            std::ostream_iterator<int>( std::cout, " " ) );
    std::cout << std::endl;

    copy( counting_range( 1, 5 ) | reversed | transformed( bind( f, _1 ) ),
            std::ostream_iterator<int>( std::cout, " " ) );
    std::cout << std::endl;
}

Which is...

8 6 4 2
1768406272 1768406272 1768406272 1768406272

I'm using Gcc 4.6.2 & Boost 1.53

Thx All.

_______________________________________________
Boost-users mailing list
[hidden email]
http://lists.boost.org/mailman/listinfo.cgi/boost-users
Reply | Threaded
Open this post in threaded view
|

Re: [Range, Phoenix] What am I missing...

Michel MORIN
Robert Jones wrote:
>     copy( counting_range( 1, 5 ) | reversed | transformed( f ),
>             std::ostream_iterator<int>( std::cout, " " ) );
>     std::cout << std::endl;
>
>     copy( counting_range( 1, 5 ) | reversed | transformed( bind( f, _1 ) ),
>             std::ostream_iterator<int>( std::cout, " " ) );
>     std::cout << std::endl;
[...]
>
> Which is...
>
> 8 6 4 2
> 1768406272 1768406272 1768406272 1768406272

This is because dereferencing `reverse_iterator` of `counting_iterator`
makes a reference to a dead temporary. See #2640
  https://svn.boost.org/trac/boost/ticket/2640

Use `irange( 1, 5 )` instead.


Regards,
Michel
_______________________________________________
Boost-users mailing list
[hidden email]
http://lists.boost.org/mailman/listinfo.cgi/boost-users
Reply | Threaded
Open this post in threaded view
|

Re: [Range, Phoenix] What am I missing...

Robert Jones-2
On Fri, Mar 15, 2013 at 3:23 PM, Michel Morin <[hidden email]> wrote:
This is because dereferencing `reverse_iterator` of `counting_iterator`
makes a reference to a dead temporary. See #2640
  https://svn.boost.org/trac/boost/ticket/2640

Use `irange( 1, 5 )` instead.


Ok, Thx Michel.

_______________________________________________
Boost-users mailing list
[hidden email]
http://lists.boost.org/mailman/listinfo.cgi/boost-users
Reply | Threaded
Open this post in threaded view
|

Re: [Range, Phoenix] What am I missing...

Boost - Users mailing list
In reply to this post by Robert Jones-2
On 15 March 2013 at 14:51, Robert Jones <[hidden email]> wrote:
Hi Folks

Can anyone explain the output of this program...

#include <iostream>
#include <boost/range/algorithm/copy.hpp>
#include <boost/range/counting_range.hpp>
#include <boost/range/adaptor/reversed.hpp> 
#include <boost/range/adaptor/transformed.hpp> 
#include <boost/phoenix/bind/bind_function.hpp>
#include <boost/phoenix/core/argument.hpp>


int f( int i ) { return i * 2; }

int main( )
{
    using boost::adaptors::reversed;
    using boost::adaptors::transformed;
    using boost::counting_range;
    using boost::copy;
    using boost::phoenix::arg_names::_1;

    copy( counting_range( 1, 5 ) | reversed | transformed( f ),
            std::ostream_iterator<int>( std::cout, " " ) );
    std::cout << std::endl;

    copy( counting_range( 1, 5 ) | reversed | transformed( bind( f, _1 ) ),
            std::ostream_iterator<int>( std::cout, " " ) );
    std::cout << std::endl;
}

Which is...

8 6 4 2
1768406272 1768406272 1768406272 1768406272

I'm using Gcc 4.6.2 & Boost 1.53

Thx All.

Michel Morin was long ago kind enough to diagnose this problem as being due to https://svn.boost.org/trac/boost/ticket/2640
which is about whether iterators may and should own their referents, but my trac-foo is not quite up to being certain 
whether this was ever fixed or indeed could or should be fixed.

I'd appreciate a quick sentence confirming the current situation.

Thx, Rob.

_______________________________________________
Boost-users mailing list
[hidden email]
https://lists.boost.org/mailman/listinfo.cgi/boost-users
Reply | Threaded
Open this post in threaded view
|

Re: [Range, Phoenix] What am I missing...

Boost - Users mailing list
On 24 April 2017 at 20:04, Robert Jones via Boost-users <[hidden email]> wrote:

Michel Morin was long ago kind enough to diagnose this problem as being due to https://svn.boost.org/trac/boost/ticket/2640
which is about whether iterators may and should own their referents, but my trac-foo is not quite up to being certain 
whether this was ever fixed or indeed could or should be fixed.

I'd appreciate a quick sentence confirming the current situation.

I don't think it's been fixed in range v2.
New efforts for ranges have mostly been happening in range v3, which has decided to be a standalone library instead of Boost. In that model, the equivalent of counting_range (iota), has iterators whose operator* return by value.

_______________________________________________
Boost-users mailing list
[hidden email]
https://lists.boost.org/mailman/listinfo.cgi/boost-users