I have a non-copyable class A
that I want to move into a vector of tuples (see code below). I understand why the code below is not working, but I'm wondering if there is a clever way to make it work without changing the implementation of my class. Any help is greatly appreciated. Thanks :)
#include <iostream>
#include <vector>
#include <tuple>
class A {
public:
int val1;
int val2;
A(int v1, int v2): val1(v1), val2(v2) {}
// make non-copyable
A(const A &other) = delete;
A &operator=(const A &other) = delete;
};
int main() {
std::vector<std::tuple<int, int, A>> my_as;
my_as.emplace_back(4, 3, A(4, 2)); // Error, how to fix this?
return 0;
}
One possible way to solve this may be to use unique pointers like this.
#include <iostream>
#include <vector>
#include <tuple>
#include <memory>
class A {
public:
int val1;
int val2;
A(int v1, int v2): val1(v1), val2(v2) {}
// make non-copyable
A(const A &other) = delete;
A &operator=(const A &other) = delete;
};
int main() {
std::vector<std::tuple<int, int, std::unique_ptr<A>>> my_as;
my_as.emplace_back(4, 3, std::make_unique<A>(4, 2));
return 0;
}