How can I call a set of variadic base class constructors based on tagged argument packs?

前端 未结 2 1172
臣服心动
臣服心动 2021-01-05 20:48

I\'d like to be able to do this:

template
struct A {
  A(int i) { }
};

template
struct B {
  B() { }
  B(const char*         


        
2条回答
  •  生来不讨喜
    2021-01-05 21:18

    I think I understand what you want. std::pair has a similar feature:

    std::pair p(std::piecewise_construct
                          , std::forward_as_tuple(foo, bar)
                          , std::forward_as_tuple(qux) );
    // p.first constructed in-place as if first(foo, bar) were used
    // p.second constructed in place as if second(qux) were used
    

    As you can see this has a lot of benefits: exactly one T and U construction each takes place, neither T and U are required to be e.g. MoveConstructible, and this only costs the constructions of two shallow tuples. This also does perfect forwarding. As a warning though, this is considerably harder to implement without inheriting constructors, and I will use that feature to demonstrate a possible implementation of a piecewise-constructor and then attempt to make a variadic version of it.

    But first, a neat utility that always come in handy when variadic packs and tuples are involved:

    template
    struct indices {
        using next = indices;
    };
    
    template
    struct build_indices {
        using type = typename build_indices::type::next;
    };
    template<>
    struct build_indices<0> {
        using type = indices<>;
    }
    
    template
    constexpr
    typename build_indices<
        // Normally I'd use RemoveReference+RemoveCv, not Decay
        std::tuple_size::type>::value
    >::type
    make_indices()
    { return {}; }
    

    So now if we have using tuple_type = std::tuple; then make_indices() yields a value of type indices<0, 1, 2, 3>.

    First, a non-variadic case of piecewise-construction:

    template
    class pair {
    public:
        // Front-end
        template
        pair(std::piecewise_construct_t, Ttuple&& ttuple, Utuple&& utuple)
            // Doesn't do any real work, but prepares the necessary information
            : pair(std::piecewise_construct
                       , std::forward(ttuple), std::forward(utuple)
                       , make_indices(), make_indices() )
         {}
    
    private:
        T first;
        U second;
    
        // Back-end
        template
        pair(std::piecewise_construct_t
                 , Ttuple&& ttuple, Utuple&& utuple
                 , indices, indices)
            : first(std::get(std::forward(ttuple))...)
            , second(std::get(std::forward(utuple))...)
        {}
    };
    

    Let's try plugging that with your mixin:

    template class... Mixins>
    struct Mix: Mixins>... {
    public:
        // Front-end
        template
        Mix(std::piecewise_construct_t, Tuples&&... tuples)
            : Mix(typename build_indices::type {}
                      , std::piecewise_construct
                      , std::forward_as_tuple(std::forward(tuples)...)
                      , std::make_tuple(make_indices()...) )
        {
            // Note: GCC rejects sizeof...(Mixins) but that can be 'fixed'
            // into e.g. sizeof...(Mixins) even though I have a feeling
            // GCC is wrong here
            static_assert( sizeof...(Tuples) == sizeof...(Mixins)
                           , "Put helpful diagnostic here" );
        }
    
    private:
        // Back-end
        template<
            typename TupleOfTuples
            , typename TupleOfIndices
            // Indices for the tuples and their respective indices
            , int... Indices
        >
        Mix(indices, std::piecewise_construct_t
                , TupleOfTuples&& tuple, TupleOfIndices const& indices)
            : Mixins>(construct>>(
                std::get(std::forward(tuple))
                , std::get(indices) ))...
        {}
    
        template
        static
        T
        construct(Tuple&& tuple, indices)
        {
            using std::get;
            return T(get(std::forward(tuple))...);
        }
    };
    

    As you can see I've gone one level higher up with those tuple of tuples and tuple of indices. The reason for that is that I can't express and match a type such as std::tuple...> (what's the relevant pack declared as? int...... Indices?) and even if I did pack expansion isn't designed to deal with multi-level pack expansion too much. You may have guessed it by now but packing it all in a tuple bundled with its indices is my modus operandi when it comes to solving this kind of things... This does have the drawback however that construction is not in place anymore and the Mixins<...> are now required to be MoveConstructible.

    I'd recommend adding a default constructor, too (i.e. Mix() = default;) because using Mix m(std::piecewise_construct, std::forward_as_tuple(), std::forward_as_tuple()); looks silly. Note that such a defaulted declaration would yield no default constructor if any of the Mixin<...> is not DefaultConstructible.

    The code has been tested with a snapshot of GCC 4.7 and works verbatim except for that sizeof...(Mixins) mishap.

提交回复
热议问题