I have a std::variant whose alternatives are all pointers.
class a {
public:
int* placeholder;
a(a& _copy) { /* Does a deep copy */ }
}
class b {
// For demonstrational purposes, identical to a.
}
int main() {
std::vector<std::variant<a*, b*>> instances = // initialise vector, guaranteed no nullptrs.
}
Now, for instance, if I want to push a deep copy of the first element to the back, I cannot simply do instances.push_back(instances[0]), because that does a shallow copy of the vector.
Of course, the obvious solution would be to check for all alternatives, then get_if, create a copy, and return the copy. However, I think there has to be a better way. How should something as such be done?
I am also open to solutions that do not use variants.
As mentioned in comments, you can use
std::visit. I strongly suggest to use smart pointers.Output:
I'll leave it to you to modify the code in case each variant requires a different way to copy. The above is the simple case where all types can be handled by a templated lambda. For the case of actually requiring distinct branches for each variant I refer you to documentation (eg https://en.cppreference.com/w/cpp/utility/variant/visit). Most importantly (as pointed out by user17732522 in their comment), this
std::make_unique<std::remove_reference_t<decltype(*x)>>(*x)is the wrong way to copy astd::unique_ptr<Base>managing aDerivedinstance.