2

I make large tuple with std::make_tuple function.

something like this

template <class ...T>
QCborArray array(const T&... args) {
    return {args...};
}

but with tuple instead of parameter pack

LightSith
  • 795
  • 12
  • 27
  • 1
    An `std::initializer_list` has to be of a single type see https://en.cppreference.com/w/cpp/utility/initializer_list – Richard Critten Sep 23 '21 at 16:54
  • @RichardCritten C++ automatically calls QCborValue's constructor for all values making them of same type https://doc.qt.io/qt-5/qcborarray.html#QCborArray-2 – LightSith Sep 23 '21 at 17:01

1 Answers1

5

You can use std::apply and a variadic lambda to do this. That would look like

template <class Tuple>
QCborArray array(Tuple&& tuple) {
    return std::apply([](auto&&... args) { return QCborArray{args...}; },
                      std::forward<Tuple>(tuple));
}
NathanOliver
  • 171,901
  • 28
  • 288
  • 402
  • Thanks. Before, posting question, I tried std::apply with function but it did not worked https://hatebin.com/gygiwasazu. Also why you added std::forward. your previous code without it was working as expected – LightSith Sep 23 '21 at 17:40
  • 1
    @尤金寶漢子 I added the `std::forward` to enable perfect forwarding. Without it, moves turn into copies and that could hurt performance. – NathanOliver Sep 23 '21 at 17:42
  • @尤金寶漢子 Also, I can't visit that link. Could you share the code using this link? http://coliru.stacked-crooked.com/ – NathanOliver Sep 23 '21 at 17:43
  • https://coliru.stacked-crooked.com/a/e2856178439b99c0. – LightSith Sep 23 '21 at 18:04
  • 1
    @尤金寶漢子 That doesn't work because `_from` is a function template, so the compiler doesn't know which instantiation of the template to use. You would either have to cast it to the correct function pointer type, or wrap it in a lambda like `return std::apply([](auto&&... args){ return _from(std::forward(args)...); }, tuple);` – NathanOliver Sep 23 '21 at 18:11