jeudi 30 janvier 2020

Why does gcc gives me maybe-uninitialized warning for deque::insert with a filtered range

Why does gcc gives me maybe-uninitialized warning for deque::insert with a filtered range when compiling with -O2 and -DNDEBUG

#include <deque>
#include <vector>
#include <boost/range/adaptor/filtered.hpp>

auto foo(){
    std::deque<int> d{3,4};
    std::vector<int> v{0,2};

    const auto b = 1;

    using boost::adaptors::filtered;
    auto r = v | filtered([&b](auto i){return i>b;});

    d.insert(std::end(d),r.begin(),r.end());
    return d;
}

Compiler gcc 6.3 , flag -std=c++14 -O2 -Wall -Werror -DNDEBUG

This is the warning:

<source>: In member function 'void std::deque<_Tp, _Alloc>::_M_insert_aux(std::deque<_Tp, _Alloc>::iterator, _ForwardIterator, _ForwardIterator, std::deque<_Tp, _Alloc>::size_type) [with _ForwardIterator = boost::iterators::filter_iterator<boost::range_detail::default_constructible_unary_fn_wrapper<foo()::<lambda(auto:1)>, bool>, __gnu_cxx::__normal_iterator<int*, std::vector<int> > >; _Tp = int; _Alloc = std::allocator<int>]':
<source>:12:48: error: '*((void*)(& __mid)+16).foo()::<lambda(auto:1)>::<b capture>' may be used uninitialized in this function [-Werror=maybe-uninitialized]
     auto r = v | filtered([&b](auto i){return i>b;});
                                               ~^~
In file included from /opt/compiler-explorer/gcc-6.3.0/include/c++/6.3.0/deque:66:0,
                 from <source>:1:
/opt/compiler-explorer/gcc-6.3.0/include/c++/6.3.0/bits/deque.tcc:768:24: note: '*((void*)(& __mid)+16).foo()::<lambda(auto:1)>::<b capture>' was declared here
       _ForwardIterator __mid = __first;

If b is captured by value, then the warning is gone.

https://godbolt.org/z/gPyF6r

Aucun commentaire:

Enregistrer un commentaire