3

I am trying to wrap a boost range that uses the boost transformed adapter into a boost any range, but this does not seem to work. I constructed a minimal example to illustrate.

std::vector<int> myInts = { 1,2,3,4,5 };
boost::any_range<double,boost::forward_traversal_tag,double> range =
    myInts | boost::adaptors::transformed( []( int x ) { return static_cast<double>( x ); } );

for ( double x : range )
    std::cout << x << "\n"; 

In release mode, my VS2015 compiler keeps telling me 'returning address of local variable or temporary'. The code also fails to perform correctly when executed. In debug mode everything is fine.

I think that somehow the any_range fails to understand that the transformed adaptor returns by value, even though I explicitly set the Reference template parameter to double instead of the default double&.

What am I doing wrong with the any_range? (Using boost 1.64.0)

Ajay
  • 18,086
  • 12
  • 59
  • 105
  • If you replace the `boost::any_range` with `auto`, does the error remain? Just trying to determine if the problem is in the any_range, or if it's something earlier. – Dave S May 29 '17 at 16:25
  • If I replace the `boost::any_range` with `auto` the error is gone. The error only occurs when I use the `boost::any_range` to type erase the transform iterator. – Johan van Rooij May 29 '17 at 16:30

1 Answers1

1

You need to change your range declaration to boost::any_range<const double, boost::forward_traversal_tag, const double>, as the type deduction system needs to realize your range is read-only.

Dave S
  • 20,507
  • 3
  • 48
  • 68