2
#include <iostream>
using namespace std;

template <class T, class... Other>
auto sum(T& first, Other... other)
{
    T mas[] = { other... };
    cout << "size: " << sizeof...(other) << endl;
    //T *f = other...;
    for (int m : mas)
        first += m;
    return first;
}

int main()
{
    int summa = 0;
    sum(summa, 1, 2, 3, 4, 5, 6, 7);
    cout << "sum: " << summa << endl;

    return 0;
}

There is a short piece of code that outputs the following:

size: 7
sum: 28

The question is very simple and quick to get the same answer: How do I access element by element each variable accounting parameter other? I tried to create a pointer, but it constantly complains, in short, I don’t know how it looks syntactically.

I will make a reservation right away that I am not interested in how to decompose the elements into an array, I myself know how exactly I should refer to each element exactly other.

More precisely, how to expand the parameter package without recursion and not decomposing the elements into an array?

JeJo
  • 30,635
  • 6
  • 49
  • 88
Fedor
  • 37
  • 5

2 Answers2

3

How to expand the parameter package without recursion?

You have since C++17-fold expression for this needs. Using it, your function will simply be

template <class T, class... Other>
auto sum(T& first, const Other&... other)
{
    first = (other + ... + first);
    return first;
}

or without the redundant variable summa may be:

#include <type_traits>  // std::common_type_t

template <class... Other>
constexpr auto sum(const Other&... other) /* noexcept */
{
    return (other + ... + std::common_type_t<Other...>{});
}

See a demo


How do I access element by element each variable accounting parameter other?

You can apply fold expression along with an immediately invoking lambda function as follows:

template <typename ReType, typename... Other>
auto do_something_with_args(ReType& ret, Other&&... other)
{
    ([&ret](auto /* const& */ arg) {
        // do something with each arg and ret
        ret += arg; // example
        std::cout << arg << '\n';
        }(std::forward<Other>(other)), ...);
    return ret;
}

See a demo


If you do not have access to C++17, then there are tweaks/ alternatives, which have been mentioned in the following posts:

JeJo
  • 30,635
  • 6
  • 49
  • 88
  • I don’t want to implement it this way, I need to access it element by element, for this I need to receive data for each element separately – Fedor May 11 '22 at 17:44
  • Elements may not match in type, this function is just an example to make it clear what I need – Fedor May 11 '22 at 17:46
0

How about this solution?

template <class T, class... Other>
auto sum(T& first, Other... other)
{
    std::apply([&first](auto &&... i){(..., (first += i)); }, std::make_tuple(std::forward<Other>(other)...));
    return first;
}
73v3n71n0
  • 18
  • 3