I'm looking to create an inheritance-based tuple, similar to how it was done in https://stackoverflow.com/a/52208842/1284735 except with constructors.
I think my regular constructor seems to work fine but when I attempt to use the copy constructor, the compiler says that I'm not providing any argument to the function. What am I missing?
tuple.cpp:41:5: note: candidate constructor not viable: requires single argument 't', but no arguments were provided TupleImpl(TupleImpl& t):
template<size_t Idx, typename T> struct TupleLeaf { T val; TupleLeaf() = default; // I think should also have proper constructors but left like this for simplicity TupleLeaf(T t): val(t) {} }; template<size_t Idx, typename... Args> struct TupleImpl; template<size_t Idx, typename T, typename... Args> struct TupleImpl<Idx, T, Args...>: TupleLeaf<Idx, T>, TupleImpl<Idx + 1, Args...> { template<typename F, typename... Rest> TupleImpl(F&& val, Rest&&... args): TupleLeaf<Idx, T>(std::forward<F>(val)), TupleImpl<Idx + 1, Args...>(std::forward<Rest>(args)...) {} TupleImpl(TupleImpl& t): TupleLeaf<Idx, T>(static_cast<TupleLeaf<Idx, T>>(t).val), TupleImpl<Idx + 1, Args...>(t) {} }; template<size_t Idx> struct TupleImpl<Idx> { TupleImpl() = default; TupleImpl(TupleImpl<Idx> &t) {} }; template<typename... Args> using Tuple = TupleImpl<0, Args...>; int main() { Tuple<int, char, string> tup{1, 'a', "5"}; // Works okay Tuple<int, char, string> x = tup; // Fails here }
TupleImpl(TupleImpl& t)tto be an lvalue and match the constructor param type worked.TupleImpl<Idx + 1, Args...>(static_cast<TupleImpl<Idx + 1, Args...>&>(t)). The first constructor seems to be matching after the copy constructor call otherwise