I have an std::vector containing a variant class. I want to construct a tuple with the same data. Is this possible? The normal construction methods for a tuple seem quite restrictive.
//In reality, I'm using JUCE::var.
// SimpleVariant is here just to make the example code more explicit.
struct SimpleVariant
{
SimpleVariant(int i) : a(i), b("") {}
SimpleVariant(const std::string& s) : a(0), b(s) {}
operator int() const { return a; }
operator std::string() const { return b; }
private:
int a;
std::string b;
};
template <typename... T>
struct VariantTuple
{
VariantTuple(const std::vector<SimpleVariant>& v)
{
// how do I initialize the tuple here?
}
private:
std::tuple<T...> tuple;
};
std::vector<SimpleVariant> v{ SimpleVariant(1),
SimpleVariant(2),
SimpleVariant("a") };
VariantTuple<int, int, std::string> t (v);
Some clarifications based on the comments:
I do not need the tuple to match the array term by term, or to deduce the types from the given array. I want to take a given array and then extract Variants that match a certain type. So, for instance, given the above array v
, I would like to be able to construct a VariantTuple<int, std::string>
, and have it match the terms "1" and "a". This introduces a host of other problems beyond the scope of my original question. But the question I'm interested in right now is whether it is even possible to construct a tuple based on an array in the first place.