Hi Robert,

I do not know the specifics of Boost.Range, but I believe what you are trying to do here is in fact undefined behaviour for initializer_lists. Reported defect DR1290 (see reference 1) explains the idea behind the temporary storage of an initializer_list in more detail. Some compilers (notably Clang) 'exploit' this undefined behaviour in order to further optimize the usage of initializer_lists. On the other hand, using it to initialize a vector and using this vector (which is more strict on the lifetime of its storage) is fully defined and therefore works as expected.

As a result, Boost.Range, which does not necessarily require copy semantics and therefore uses a reference to the collection (see reference 2), can only do the right thing while the storage behind the object still exists.

Kindest Regards,

Bart 

References:
1. http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html#1290
2. https://www.boost.org/doc/libs/1_66_0/libs/range/doc/html/range/concepts/overview.html

2018-04-16 23:06 GMT+02:00 Robert Jones via Boost-users <boost-users@lists.boost.org>:
Hi All

Does the Boost.Range library support initializer_list as a range type? I thought anything with begin() and end() methods was fair game, but this code doesn't work, unless I'm doing it wrong.

#include <iostream>
#include <vector>
#include <boost/range/adaptor/reversed.hpp>

int main( )
{
  using namespace std;
  using namespace boost::adaptors;

  vector<int> v { 1, 2, 3 };
  for( int i : v           | reversed ) { cout << i << " "; } // Ok
  for( int i : { 1, 2, 3 } | reversed ) { cout << i << " "; } // Bad
}

Kind Regards

Rob.


_______________________________________________
Boost-users mailing list
Boost-users@lists.boost.org
https://lists.boost.org/mailman/listinfo.cgi/boost-users