This code:
#include <memory>
#include <vector>
#include <algorithm>
struct Foo
{
int bar;
Foo(const int val) :
bar(val)
{
}
};
int main() {
std::vector<std::unique_ptr<Foo>> vec;
vec.emplace_back(std::make_unique<Foo>(42));
Foo* ptr = vec.back().get();
auto& it = std::find_if(vec.begin(), vec.end(), [&](std::unique_ptr<Foo>& p)
{
return p.get() == ptr;
});
if (it != vec.end())
{
vec.erase(it);
}
return 0;
}
Works fine in MSVC, but errors out in GCC 5.1:
prog.cpp: In function 'int main()':
prog.cpp:19:25: error: invalid initialization of non-const reference of type '__gnu_cxx::__normal_iterator*, std::vector > >&' from an rvalue of type '__gnu_cxx::__normal_iterator*, std::vector > >' auto& it = std::find_if(vec.begin(), vec.end(), [&](std::unique_ptr& p)
std::vector
of std::unique_ptr
correctly?gcc is correct here. You can't initialize a lvalue reference with an rvalue, and you're doing it for the it
reference to an iterator (std::find_if
returns an rvalue)
auto& it = std::find_if(vec.begin(), vec.end(), [&](std::unique_ptr<Foo>& p)
^
Either make it an object:
auto it = std::find_if(vec.begin(), vec.end(), [&](std::unique_ptr<Foo>& p)
demo
or a const reference:
auto const& it = std::find_if(vec.begin(), vec.end(), [&](std::unique_ptr<Foo>& p)
demo
Other than that, your code for erasing an element from a vector is correct
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With