Boost.Range and initializer_list
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
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/ove... 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
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
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
Right, a full and comprehensive explanation. Many Thanks. Kind Regards Rob. On 17 April 2018 at 12:17, Michel Morin via Boost-users < boost-users@lists.boost.org> wrote:
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 Boost-users@lists.boost.org https://lists.boost.org/mailman/listinfo.cgi/boost-users
participants (3)
-
Bart Verhagen
-
Michel Morin
-
Robert Jones