c++ Unpacking parameter pack from template arguments

prestokeys

How to achieve want I want below? The paramater pack I want to unpack is not in a function argument list but template argument list.

#include <iostream>
#include <array>

const std::size_t SIZE = 10;

template <int...ARGS>
std::array<bool, SIZE> func() {
    std::array<bool, SIZE> b;
    // I want to set b[n] = true, where n takes on all values from ARGS...
    // what to put in here???
    return b;
}

// Example of what I want to achieve:
int main() {
    const std::array<bool, SIZE> b = func<1,3,7>();
    // I want b[1]==true, b[3]==true, b[7]==true, all others false
    for (int x: b) std::cout << x << std::endl;
}

I have to use this form for func (instead of func(1,3,7)) to get my bigger program working (I'm dealing with multiple inheritance issues).

Sam Cristall

Recursive template solution:

// recursive helper struct
template <int n, int First, int ...Rest>
struct helper {
  static void funcImpl(std::array<bool, SIZE>& temp) {
    temp[First] = true;
    helper<n - 1, Rest...>::funcImpl(temp);
  }
};

// partial specialization to catch base case
template <int First>
struct helper<0, First> {
  static void funcImpl(std::array<bool, SIZE>& temp) {
    temp[First] = true;
  }
};

template <int ...Args>
std::array<bool, SIZE> func() {
    std::array<bool, SIZE> b = {}; // 0 inititalize array
    helper<sizeof...(Args) - 1, Args...>::funcImpl(b);
    return b;
}

EDIT: A super simplified version inspired by iavr's solution:

template <int... A>
std::array<bool, SIZE> func() {
    std::array<bool, SIZE> b = {};
    auto values = {A...};
    std::for_each(values.begin(), values.end(), [&](int n){b[n] = true;});
    return b;
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Unpacking Parameter Pack in C++

From Dev

Unpacking arguments of a functional parameter to a C++ template class

From Dev

Compile time array from C++ template parameter pack

From Dev

C++11 variadic template unpacking arguments in functor

From Dev

Unpacking arguments from tuples

From Dev

Unpacking arguments from argparse

From Dev

Deduce template parameter pack from function call

From Dev

Variadic template unpacking arguments in typedef

From Dev

variadic template unpacking arguments to typename

From Dev

variadic template unpacking arguments to typename

From Dev

Why is this substitution of variadic template parameter failing ? (pack before fixed arguments)

From Dev

C++ passing template parameter pack from one variadic template to another causes compiler error

From Dev

Unpacking parameter packs in template aliases

From Dev

Declaring a function with types of arguments taken from a foreign parameter pack

From Dev

How to extract a value from a variadic template parameter pack by index?

From Dev

How to extract params from template function parameter pack?

From Dev

How can a type be removed from a template parameter pack?

From Dev

How can a type be removed from a template parameter pack?

From Dev

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

From Dev

Deducing template arguments during partial ordering when parameters are function parameter pack

From Dev

Inspect template parameter pack in gdb

From Dev

Parameter pack with default template argument

From Dev

Cumulative Product of Template Parameter Pack

From Dev

Specialize on default template parameter pack

From Dev

Dynamically build a template parameter pack

From Dev

a variadic template as a template parameter without pack expansion

From Dev

Function template parameter pack not at the end of the parameter list

From Dev

Inserting any number of types into a pack of template arguments

From Dev

How to match empty arguments pack in variadic template

Related Related

  1. 1

    Unpacking Parameter Pack in C++

  2. 2

    Unpacking arguments of a functional parameter to a C++ template class

  3. 3

    Compile time array from C++ template parameter pack

  4. 4

    C++11 variadic template unpacking arguments in functor

  5. 5

    Unpacking arguments from tuples

  6. 6

    Unpacking arguments from argparse

  7. 7

    Deduce template parameter pack from function call

  8. 8

    Variadic template unpacking arguments in typedef

  9. 9

    variadic template unpacking arguments to typename

  10. 10

    variadic template unpacking arguments to typename

  11. 11

    Why is this substitution of variadic template parameter failing ? (pack before fixed arguments)

  12. 12

    C++ passing template parameter pack from one variadic template to another causes compiler error

  13. 13

    Unpacking parameter packs in template aliases

  14. 14

    Declaring a function with types of arguments taken from a foreign parameter pack

  15. 15

    How to extract a value from a variadic template parameter pack by index?

  16. 16

    How to extract params from template function parameter pack?

  17. 17

    How can a type be removed from a template parameter pack?

  18. 18

    How can a type be removed from a template parameter pack?

  19. 19

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

  20. 20

    Deducing template arguments during partial ordering when parameters are function parameter pack

  21. 21

    Inspect template parameter pack in gdb

  22. 22

    Parameter pack with default template argument

  23. 23

    Cumulative Product of Template Parameter Pack

  24. 24

    Specialize on default template parameter pack

  25. 25

    Dynamically build a template parameter pack

  26. 26

    a variadic template as a template parameter without pack expansion

  27. 27

    Function template parameter pack not at the end of the parameter list

  28. 28

    Inserting any number of types into a pack of template arguments

  29. 29

    How to match empty arguments pack in variadic template

HotTag

Archive