Questions tagged [parameter-pack]

171 questions
26
votes
1 answer

Problem with order of evaluation in parameter pack expansion

I wrote a template that takes an istream& and a function and is supposed to extract all parameters for this function from the istream, call the function with these parameters and return the result. It all works fine, except for the order of…
Lukas-T
  • 11,133
  • 3
  • 20
  • 30
15
votes
1 answer

Overload resolution for a function with multiple parameter packs in C++

A C++ function can have more than one parameter packs. Although it does not look very practical thing, it is still interesting to know language rules about them. For example, in case of two overloads: constexpr int f(auto...) { return 1; } constexpr…
Fedor
  • 17,146
  • 13
  • 40
  • 131
9
votes
3 answers

Expand parameter pack with index using a fold expression

I've got a template function taking a parameter pack. I want to expand it into calls to a second function while also supplying the index of the item in the pack. I probably can work out how to do it with recursion but I would like to try do it…
Joe
  • 5,394
  • 3
  • 23
  • 54
6
votes
1 answer

Return the addition of variadic parameter pack

Let's say I have a function sum that takes a variadic parameter pack. This function needs to ADD UP all of the parameters of the parameter pack using the operator +. NOTE: It CANNOT use the operator += in any way and can use only operator + (as the…
Amolgorithm
  • 697
  • 2
  • 20
6
votes
1 answer

How to perfect forward the elements of a function parameter pack in C++

I am having trouble understanding how to forward the elements of a parameter pack in C++. Please take for example the code below: #include void print(const int& value) { std::cout << "print(const int& value) - " << value <<…
6
votes
1 answer

Passing parameter pack in constexpr

i am trying to determine the size of all passed objects at compile time and then abort the build process via static_assert when a maximum size is exceeded. #include template class Test { public: T value; constexpr size_t…
5
votes
1 answer

MSVC vs Clang/GCC bug during overload resolution of function templates one of which contains a parameter pack

I was using parameter pack when I noticed that one such case(shown below) compiles fine in gcc and clang but not in msvc: template void func(T a, T b= T{}) { } template void func(T a, S... b) { } int main() { …
Jason
  • 36,170
  • 5
  • 26
  • 60
5
votes
1 answer

Where does C++20 prohibit sizeof...T (without parentheses)

Both g++ and clang++ reject the use of sizeof... when the argument is not parenthesized. For example, the following code is rejected: template auto packsize = sizeof...T; $ g++ -std=c++20 -ggdb -O -Wall -Werror -c -o z.o…
5
votes
2 answers

non type template parameter pack expansion

I simply would print out a non type template parameter pack. But I can't find a valid expansion if I want to have spaces in between the elements. Example: template < int ... N > struct X { static void Check() { // this will become…
Klaus
  • 24,205
  • 7
  • 58
  • 113
5
votes
2 answers

C++: Get head and tail of a parameter pack

How to obtain the first n elements of a parameter pack? Or the last n elements, or a slice of elements in [n, n+1, ..., m) in general? For instance: head<3>(1, 2.0f, "three", '4') => make_tuple(1, 2.0f, "three") tail<2>(1, 2.0f, "three", '4') =>…
4
votes
0 answers

Two parameter packs in a function works. Is that "undefined behaviour"?

I have a function with two parameter packs where I can control what ends up in each one of them. #include using namespace std; template< typename... Args0, typename... Args1> void func( Args0&&... args0, Args1&&... args1 ) { cout <<…
Generic Name
  • 1,083
  • 1
  • 12
  • 19
4
votes
2 answers

Parameter pack works in msvc but not in gcc and clang

I recently learnt about parameter packs. Then I wrote the following program that compiles with msvc but not with clang and gcc. Live demo: #include template int func(T (&arr...)[rows]) { return 5; …
Alan
  • 116
  • 9
4
votes
2 answers

C++ Paramater pack expansion over chained function calls

It is common for libraries to have types which return instances of themselves from member functions to encourage chaining calls. For example, nlohmann json: auto my_data = my_json_object["first key"]["second key"]; Is there some way to call a…
xzaton jw
  • 41
  • 3
4
votes
1 answer

pack expansion used as argument for non-pack parameter of concept

template concept A = true; template concept B = A; Clang and gcc complains 'pack expansion used as argument for non-pack parameter of concept' at the 2nd line. MSVC compiles. Are the code well-formed or…
wanghan02
  • 1,227
  • 7
  • 14
4
votes
1 answer

Is there any regular pattern to figure out where to put ... in templates?

I got confused with the location where the ... should be put when using template parameter packs. For example, in template parameter list, we should use typename ...Ts, while in a parameter list, it becomes Ts...ts. When instantiate a template, it…
user2269707
1
2 3
11 12