Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Insert unique_ptr in vector as pair

I have following vector:

std::vector< std::pair< std::unique_ptr<CEdit>, CRect >> m_editCtrls;

I'm trying to insert here some data:

std::unique_ptr<CEdit> edit = std::make_unique<CEdit>();
CRect rectEdit;
//Init edit....
//1 - This doesn't work
std::pair< std::unique_ptr<CEdit>, CRect > pair = std::make_pair<std::unique_ptr<CEdit>, CRect>(std::move(edit), rectEdit);
//2 - This also
m_editCtrls.insert(std::make_pair(std::move(edit), rectEdit));

In first case I'm getting error - no instance of function template, in second - no instance of overloaded function.

Which is the proper way to insert this pointer into the vector?

Thank's for help.

like image 661
drewpol Avatar asked Jun 25 '26 12:06

drewpol


1 Answers

Don't pass template arguments to std::make_pair. Just let them be deduced. See this video(link) by Steven Lavavej. (thanks to Passer By for finding the link)

std::pair<std::unique_ptr<CEdit>, CRect> pair = std::make_pair(std::move(edit), rectEdit);

std::vector does not have a function named insert that only takes one argument. You have to pass an iterator for a position. Perhaps you were looking for push_back?

m_editCtrls.push_back(std::make_pair(std::move(edit), rectEdit));

Or even better, emplace_back.

m_editCtrls.emplace_back(std::move(edit), rectEdit);
like image 181
Benjamin Lindley Avatar answered Jun 27 '26 02:06

Benjamin Lindley