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 [hidden email] https://lists.boost.org/mailman/listinfo.cgi/boost-users |
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: 2018-04-16 23:06 GMT+02:00 Robert Jones via Boost-users <[hidden email]>:
_______________________________________________ Boost-users mailing list [hidden email] https://lists.boost.org/mailman/listinfo.cgi/boost-users |
In reply to this post by Boost - Users mailing list
Robert Jones via Boost-users wrote:
> for( int i : { 1, 2, 3 } | reversed ) { cout << i << " "; } // Bad Boost.Range is not relevant here (other than the prvalue problem). `{...} | ...` is not allowed in C++. See https://bugs.llvm.org/show_bug.cgi?id=13069 In C++2a, you can use this for (auto rng = {...}; auto&& x : rng | ...) {...} thanks to http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2017/p0614r1.html. Regards, Michel _______________________________________________ Boost-users mailing list [hidden email] https://lists.boost.org/mailman/listinfo.cgi/boost-users |
Right, a full and comprehensive explanation. Many Thanks. Kind Regards Rob. On 17 April 2018 at 12:17, Michel Morin via Boost-users <[hidden email]> wrote: Robert Jones via Boost-users wrote: _______________________________________________ Boost-users mailing list [hidden email] https://lists.boost.org/mailman/listinfo.cgi/boost-users |
Free forum by Nabble | Edit this page |