Let's consider an std::unordered_set
of std::unique_ptr<T>
as an example. Can I move an element of the set elsewhere ?
#include <unordered_set>
#include <iostream>
#include <memory>
#include <vector>
int main()
{
std::unordered_set<std::unique_ptr<int>> mySet;
mySet.insert(std::make_unique<int>(1));
mySet.insert(std::make_unique<int>(2));
mySet.insert(std::make_unique<int>(3));
std::vector<std::unique_ptr<int>> myVector;
for (auto&& element : mySet)
{
std::cout << *element << std::endl;
//myVector.push_back(element); won't compile as you can only get a const ref to the key
}
}
I have a very practical example of code where I would like to do this but am reduce to use a std::shared_ptr
. Do you know of another (better ?) alternative ?
Aucun commentaire:
Enregistrer un commentaire