Search code examples
c++c++17template-meta-programmingstdtupleargument-unpacking

Unpack tuple to member initialization or superclass constructor


Is it possible to initialize a member of a class (or call superclass constructor) by using the arguments contained in a tuple? Please note that I am aware of std::make_from_tuple() and std::apply() but they cannot be used in this scenario.

Consider sometype is non copyable, non default constructible, non movable, non assignable, non anything else. The only way to instantiate it is by using using the arguments that are provided as a std::tuple. Can this be done?

My attempt fails as appereantly "expansion pattern contains no argument pack" though the index_sequence generate should be one.

class node {
    public:
    sometype value;
    template <typename... Args>
    explicit node(const std::tuple<Args...>& args_tuple) :
            value(
                std::get<
                    std::index_sequence_for<Args...>{}
                >(args_tuple)...
            ) {
    }
};

Solution

  • If you insist on not using std::make_from_tuple(), you need another level of indirection:

    public:
        explicit node(const std::tuple<Args...>& args_tuple) :
            node(args_tuple, std::make_index_sequence<sizeof...(Args)>{})
        { }
    
    private:
        template<class Tuple, std::size_t... is>
        node(const Tuple& args_tuple, std::index_sequence<is...>) :
            value(std::get<is>(args_tuple)...)
        { }