I am trying to emplace an std::unique_ptr
into a std::list
, but it's failing with the following error:
<source>:29:23: error: no matching function for call to 'std::__cxx11::list<std::unique_ptr<test> >::emplace(std::remove_reference<std::unique_ptr<test>&>::type)'
29 | mapList[1].emplace(std::move(t));
Following is the code:
using namespace std;
class test {
public:
int i;
};
typedef std::list<std::unique_ptr<test>> testList;
int main() {
unordered_map<int, testList> mapList;
mapList.try_emplace(1, testList());
std::unique_ptr<test> t = make_unique<test>();
mapList[1].emplace(std::move(t));
return 0;
}
Try it here: https://godbolt.org/z/Wq4hPrzPj
The error is quite explicit: There is no matching call for std::list<>::emplace()
with the arguments you provided.
Indeed, if we look carefully at the documentation, we notice that std::list<>::emplace()
takes a position as first argument (given through a std::list<>::const_iterator
).
In your case, since the list is empty, what you probably wanted was either std::emplace_back()
or std::emplace_front()
.
When in doubt, always refer to a good documentation.
Sidenote: You should really get rid of that bad using namespace std;
habit.
You can see more information on the topic here: Why is "using namespace std;" considered bad practice?.