Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7647789
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T10:33:02+00:00 2026-05-31T10:33:02+00:00

I’d like to be able to do this: template<typename Mix> struct A { A(int

  • 0

I’d like to be able to do this:

template<typename Mix>
struct A {
  A(int i) { }
};

template<typename Mix>
struct B {
  B() { }
  B(const char*) { }
};

template<template<typename> class... Mixins>
struct Mix : Mixins<Mix<Mixins...>>... {
   // This works, but forces constructors to take tuples
   template<typename... Packs>
   Mix(Packs... packs) : Packs::Type(packs.constructorArgs)... { }
};

template<template<typename> class MixinType, typename... Args>
struct ArgPack {
  typedef MixinType Type; // pretend this is actually a template alias
  tuple<Args...> constructorArgs;
  ArgPack(Args... args) : constructorArgs(args...) { }
}

template<typename... Args>
ArgPack<A, Args...> A_(Args... args) {
  return ArgPack<A, Args...>(args...);
}

template<typename... Args>
ArgPack<B, Args...> B_(Args... args) {
  return ArgPack<B, Args...>(args...);
}

Mix<A, B> m(); // error, A has no default constructor

Mix<A, B> n(A_(1)); // A(int), B()
Mix<A, B> n(A_(1), B_("hello"); // A(int), B(const char*)

How do I fill in /* mysterious code here */ to do what I want, to provide a nice interface for calling some set of constructors of mixins? I have a solution that works by making all non-null constructs actually take a tuple of args, and then overloading figures out which one to call, but I would like to avoid constraining mixin authors by making them write a constructor A(tuple), instead of just A(int, int).

Thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T10:33:04+00:00Added an answer on May 31, 2026 at 10:33 am

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

    std::pair<T, U> 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<int... Indices>
    struct indices {
        using next = indices<Indices..., sizeof...(Indices)>;
    };
    
    template<int Size>
    struct build_indices {
        using type = typename build_indices<Size - 1>::type::next;
    };
    template<>
    struct build_indices<0> {
        using type = indices<>;
    }
    
    template<typename Tuple>
    constexpr
    typename build_indices<
        // Normally I'd use RemoveReference+RemoveCv, not Decay
        std::tuple_size<typename std::decay<Tuple>::type>::value
    >::type
    make_indices()
    { return {}; }
    

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

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

    template<typename T, typename U>
    class pair {
    public:
        // Front-end
        template<typename Ttuple, typename Utuple>
        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>(ttuple), std::forward<Utuple>(utuple)
                       , make_indices<Ttuple>(), make_indices<Utuple>() )
         {}
    
    private:
        T first;
        U second;
    
        // Back-end
        template<typename Ttuple, typename Utuple, int... Tindices, int... Uindices>
        pair(std::piecewise_construct_t
                 , Ttuple&& ttuple, Utuple&& utuple
                 , indices<Tindices...>, indices<Uindices...>)
            : first(std::get<Tindices>(std::forward<Ttuple>(ttuple))...)
            , second(std::get<Uindices>(std::forward<Utuple>(utuple))...)
        {}
    };
    

    Let’s try plugging that with your mixin:

    template<template<typename> class... Mixins>
    struct Mix: Mixins<Mix<Mixins...>>... {
    public:
        // Front-end
        template<typename... Tuples>
        Mix(std::piecewise_construct_t, Tuples&&... tuples)
            : Mix(typename build_indices<sizeof...(Tuples)>::type {}
                      , std::piecewise_construct
                      , std::forward_as_tuple(std::forward<Tuples>(tuples)...)
                      , std::make_tuple(make_indices<Tuples>()...) )
        {
            // Note: GCC rejects sizeof...(Mixins) but that can be 'fixed'
            // into e.g. sizeof...(Mixins<int>) 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<Indices...>, std::piecewise_construct_t
                , TupleOfTuples&& tuple, TupleOfIndices const& indices)
            : Mixins<Mix<Mixins...>>(construct<Mixins<Mix<Mixins...>>>(
                std::get<Indices>(std::forward<TupleOfTuples>(tuple))
                , std::get<Indices>(indices) ))...
        {}
    
        template<typename T, typename Tuple, int... Indices>
        static
        T
        construct(Tuple&& tuple, indices<Indices...>)
        {
            using std::get;
            return T(get<Indices>(std::forward<Tuple>(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<indices<Indices...>...> (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<A, B> 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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
For some reason, after submitting a string like this Jack’s Spindle from a text
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have some data like this: 1 2 3 4 5 9 2 6
I would like to count the length of a string with PHP. The string
I want to count how many characters a certain string has in PHP, but
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I've got a string that has curly quotes in it. I'd like to replace
this is what i have right now Drawing an RSS feed into the php,
I would like to run a str_replace or preg_replace which looks for certain words

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.