12

Is it possible to initialize all elements of std::tuple by the same argument, using the non-default constructors of the underlying types?

template <typename... TElements>
struct Container {
    // I'd wish to be able to do something like this:
    Container(Foo foo, Bar bar)
        : tuple(foo, bar)
    {}
    std::tuple<TElements...> tuple;
};

The point is that I don't know the tuple size (it's templated by a variadic parameter), so I can't duplicate the arguments as many times as I need. The only thing I know is that all types in TElements have a constructor taking Foo and Bar as arguments and don't have a default constructor.

5
  • std::tuple<NoDefault1, NoDefault2> foo {{Foo{}, Bar{}}, {Foo{}, Bar{}}}; ?
    – Jarod42
    Mar 21, 2014 at 13:37
  • 1
    @Jarod42: I think the point is not to repeat the arguments.
    – bitmask
    Mar 21, 2014 at 13:40
  • I can't explicitly write it this way because I don't know the tuple size.
    – lizarisk
    Mar 21, 2014 at 13:40
  • What's the template parameter that controls the size of the tuple?
    – jrok
    Mar 21, 2014 at 13:44
  • I've edited my question to descripbe my problem more clearly
    – lizarisk
    Mar 21, 2014 at 13:45

3 Answers 3

14

The clearest way is just to construct each element in the tuple constructor argument list:

template <typename... TElements>
struct Container {
    Container(Foo foo, Bar bar)
        : tuple(TElements{foo, bar}...)
    {}
    std::tuple<TElements...> tuple;
};

This will result in move (or copy) constructing each element of the tuple from its corresponding constructor parameter; if this is unacceptable you could use piecewise construction:

template <typename... TElements>
struct Container {
    Container(Foo foo, Bar bar)
        : tuple(std::piecewise_construct, (sizeof(TElements), std::tie(foo, bar))...)
    {}
    std::tuple<TElements...> tuple;
};

Unfortunately in this case we have to do some kind of gymnastics (here sizeof and a comma operator) to get the variadic list TElements mentioned and ignored.

5
  • Wow, it's really that simple, how could I have missed that? :)
    – lizarisk
    Mar 21, 2014 at 14:13
  • @lizarisk variadic templates often are simpler than you'd think :)
    – ecatmur
    Mar 21, 2014 at 14:23
  • 3
    @0x499602D2 we need to repeat std::tie(foo, bar) as many times as there are elements in the tuple, so we need to expand TElements, but we don't actually need the type of each element so we have to find some way to ignore it.
    – ecatmur
    Mar 22, 2014 at 8:17
  • 1
    Just a small addition: I used your approach (thanks!) with gcc6 and got a warning that left-hand parameter of comma operator has no effect. To get rid of the warning, I wrapped sizeof(TElements) into a cast to void: (void(sizeof(TElements)), std::tie(foo, bar)).
    – Sergey
    Jun 26, 2017 at 8:17
  • 1
    Tuple doesn't work with piecewise_construct, only pair does. Nov 18, 2022 at 11:05
5

with double parameter pack expansion you can (try to) construct each element of a given tuple class with all given parameters to a function:

template <class T> struct tuple_construct_t;

template <class... Ts> struct tuple_construct_t<std::tuple<Ts...>> {
  template <class... Args>
  static std::tuple<Ts...> make_tuple(Args&&... args) {
    //this is the central part - the double pack expansion
    return std::make_tuple(Ts{args...}...);
  }
};

// a little free helper function...
template <class Tup, class... Args>
Tup construct_tuple(Args&&... args) {
    return tuple_construct_t<Tup>::make_tuple(std::forward<Args>(args)...);
}

And then somewhere in the code:

typedef std::tuple<NoDefault1, NoDefault2> myTuple;
auto t = construct_tuple<myTuple>(Foo{}, Bar{});

full working example: Link

Edit:

Since @Rakvan deleted his answer, I'll preserve the second (correct) part of it:

template <class ... Ts, class ... Args>
std::tuple<Ts...> cartesian_make_tuple(Args && ... args)
{
    return std::make_tuple(Ts{args...}...);
}

here is a working exaple

2
  • The @Rakvan's second answer seems to be correct, and it's the most simple!
    – lizarisk
    Mar 21, 2014 at 14:02
  • 1
    @lizarisk Yes, I just realized you cannot forward the arguments, because you should not construct multiple tuple elements with the same forwarded (moved) rvalue argument.
    – Arne Mertz
    Mar 21, 2014 at 14:04
2

We want to do variadic expansion (to get just the right amount of parameters), but we have to put a ‘hint’ to tie the expansion to whichever pack it is we want to match:

template<typename Dummy, typename Value>
Value depends(Value&& value)
{ return std::forward<Value>(value); }

template<typename... Elements>
void example()
{
    // naive attempt to construct all the elements from 0:
    // std::tuple<Elements...> t { 0... };

    // now expansion is tied to the Elements pack
    std::tuple<Elements...> t { depends<Elements>(0)... };

    // with two arguments:
    std::tuple<Elements...> t { { depends<Elements>(0), depends<Elements>(1) }... };
}
1
  • sorry, accidentally edited your answer instead of mine.
    – Arne Mertz
    Mar 21, 2014 at 14:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.