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)
- Which compiler is bugged?
- How do I erase a pointer from a
std::vectorofstd::unique_ptrcorrectly?
/Zaflag.