-5

I need the following meta code adapted from std::pair to std::tuple with any number of elements. I don't want to implement it separately for every possible number of elements.

template<typename A, typename B>
struct merge_pairs
{       typedef std::pair<
            decltype(typename A::first() + typename B::first()),
            decltype(typename A::second() + typename B::second())
        > type;
};
Piotr Skotnicki
  • 46,953
  • 7
  • 118
  • 160
  • you can use variadic template. google it. – Cheers and hth. - Alf Apr 21 '16 at 03:59
  • 2
    You should provide a **complete** but minimal example exemplifying the desired behavior. It's OK if it doesn't compile as long as the part that doesn't compile is the one you don't have. Make sure that otherwise it's perfect. :) – Cheers and hth. - Alf Apr 21 '16 at 04:03
  • 1
    Your question probably got closed because it shows none of your efforts to solve the problem. Right now it's a "do my job for me" question. What did you try, and where did you get stuck ? – Quentin Apr 21 '16 at 08:29
  • @Alf -- the example implementation was always there (merge_pairs). –  Apr 21 '16 at 16:22
  • @Alf -- I knew that I can use variadic templates. But I don't know how. This is what stackoverflow.com is for, isn't it? –  Apr 21 '16 at 16:24
  • @Quentin -- no it got closed by "moderators", who did not understand it. They should have simply left the question alone. If I remember correctly the question got 5 upvotes before it got closed. But what does a good dictator care about other peoples opinions.... –  Apr 21 '16 at 16:26
  • @ExcessPhase On what basis do you believe that your question being downvoted and/or closed is due to people not understanding it ? That's not how SO works, and there is no conspiracy against you. But a question without research effort and an aggressive introductory paragraph is definitely going to gather negative feedback. – Quentin Apr 21 '16 at 17:16
  • @Quentin the answer to your question is obvious. My discussion regarding this was deleted on meta.stackoverflow.com. But there was another discussion on meta.stackoverflow.com -- mine was labeled as a duplicate of this one: http://meta.stackoverflow.com/questions/251758/why-is-stack-overflow-so-negative-of-late/252077 –  Apr 21 '16 at 17:30
  • @Quentin: If you do not like my introduction, why don't you do your best, that "moderators" leave questions alone if they do not understand them. Again -- my question got 5 positive votes before getting deleted! –  Apr 21 '16 at 17:38
  • @ExcessPhase I have not seen your previous question, and have nothing to do with this. But questions are not the place to argue or foment a revolution, and they should be properly formed. That's just SO's quality standard here. – Quentin Apr 21 '16 at 17:51
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/109844/discussion-between-excessphase-and-quentin). –  Apr 21 '16 at 17:56
  • @Quentin I already solved the question before I asked it here for the second time -- but this does not matter. Sharing questions and answers is what stackoverflow.com is all about. So I don't understand your comment regarding doing research myself. –  Apr 21 '16 at 18:42

1 Answers1

2

Option #1

#include <cstddef>
#include <type_traits>
#include <utility>
#include <tuple>

template <typename A, typename B>
struct merge_tuples
{
    static_assert(std::tuple_size<A>::value == std::tuple_size<B>::value, "!");

    template <std::size_t... Is>
    static auto merge(std::index_sequence<Is...>) noexcept
        -> std::tuple<typename std::decay<decltype(std::declval<typename std::tuple_element<Is, A>::type>()
                                                 + std::declval<typename std::tuple_element<Is, B>::type>())
                               >::type...>;

    using type = decltype(merge(std::make_index_sequence<std::tuple_size<A>::value>{}));    
};

DEMO

Option #2

#include <type_traits>
#include <utility>

template <typename A, typename B>
struct merge_tuples;

template <template <typename...> class Tuple, typename... Ts, typename... Us>
struct merge_tuples<Tuple<Ts...>, Tuple<Us...>>
{
    static_assert(sizeof...(Ts) == sizeof...(Us), "!");

    using type = Tuple<typename std::decay<decltype(std::declval<Ts>()
                                                  + std::declval<Us>())
                                >::type...>;
};

DEMO 2

Piotr Skotnicki
  • 46,953
  • 7
  • 118
  • 160
  • What is the static_assert in the second example for? –  Apr 21 '16 at 13:36
  • @ExcessPhase the number of elements of both variadic packs should be equal – Piotr Skotnicki Apr 21 '16 at 13:37
  • Example 1 is using a function which is not implemented, right? Cute! –  Apr 21 '16 at 13:40
  • 1
    @ExcessPhase yes, using a function (that offers template type deduction out of the box) is often more concise than introducing a primary template and then providing a specialization. Since the function is used in an unevaluated context (inside `decltype`), you don't need a body – Piotr Skotnicki Apr 21 '16 at 13:47