Using a range as a parameter pack

User.cpp

I have a function

#include <iostream>
#include <iterator>
#include <vector>

template <typename T, typename P...> Function (T&& Sth, P&&... SthElse) {
    //Perform operations on all arguments. For example:
    std::cout << Sth << " ";
    (std::cout << ... << SthElse);
    std::cout <<"\n"; 
}

If I also have a vector

int main () {
    std::vector<int> V {1, 2, 3, 4, 5};
}

is there some way to pass the range containing my numbers to the function as a parameter pack? My desired structure would be something like

    Function(SomethingMagic(V.begin(), V.end());

where SomethingMagic turns the range into a pack to get an output in the form

1 2 3 4 5

Is there any way to convert the range in a parameter pack? Thanks in advance to anyone.

Jarod42

You cannot use runtime value for compile time one.

vector size is a runtime value, size of pack is compile time.

But, if you know size at compile time, you might do something like:

template <typename C, std::size_t ... Is>
auto to_tuple_impl(C&& c, std::index_sequence<Is...>)
{
    return std::tie(c[Is]...);
}

template <std::size_t N, typename C>
auto to_tuple(C&& c)
{
    return to_tuple_impl(std::forward<C>(c), std::make_index_sequence<N>());
}

and then

std::apply([](auto...args){Function(args...); }, to_tuple<5>(v));

Demo

or

switch (v.size())
{
    case 5: return std::apply([](auto...args){Function(args...); }, to_tuple<5>(v));
    case 42: return std::apply([](auto...args){Function(args...); }, to_tuple<42>(v));
    // ...
    default: return; // Nothing
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Checking type of parameter pack using enable_if

From Dev

Using Date range parameter in the command

From Dev

C++ using a template parameter pack to invoke multiple templated functions

From Dev

What is the easiest way to print a variadic parameter pack using std::ostream?

From Dev

Clang fails to compile parameter pack expansion using template metaprogramming

From Dev

Date range as parameter in SSRS using MDX

From Dev

Parameter pack passed by reference

From Dev

Is it possible to typedef a parameter pack?

From Dev

Parameter pack expansion fails

From Dev

Tuple to parameter pack

From Dev

Expand a parameter pack with a counter

From Dev

Expanding container to parameter pack

From Dev

Behavior of a function parameter pack

From Dev

Expand a parameter pack with a counter

From Dev

Expand parameter pack in order

From Dev

Permute the parameters in a parameter pack

From Dev

Parameter pack expansion questions

From Dev

std::min with Parameter Pack

From Dev

Pack tuple with its index range

From Dev

Difference between typename parameter pack and auto parameter pack?

From Dev

Empty parameter pack expansion different to manual empty parameter pack

From Dev

C++ Convert a parameter pack of types to parameter pack of indices

From Dev

Difference between typename parameter pack and auto parameter pack?

From Dev

Simple parameter pack expansion: expected ';'

From Dev

Inspect template parameter pack in gdb

From Dev

Parameter pack, capture clause and initializers

From Dev

Unpacking Parameter Pack in C++

From Java

Incrementing every value in a parameter pack

From Dev

Getting the type of parameters in a parameter pack

Related Related

HotTag

Archive