Questions tagged [index-sequence]

17 questions
5
votes
2 answers

Non-type template parameter for polymorphic lambda?

Is it possible to write something like this? [](std::index_sequence s) { }; Or this? [](std::index_sequence s) { } How is the syntax for this in C++14 or C++17? Or is it not possible at all? Basically,…
Johannes Schaub - litb
  • 496,577
  • 130
  • 894
  • 1,212
4
votes
1 answer

Pack expansion for std::array initialization using std::index_sequence

I need to initialize an std:array with N objects taking the same constructor arguments, as in std::vector(size_t, {args...}). From my search here I came up with this, which works: template
kamedin
  • 43
  • 3
4
votes
1 answer

How to avoid using the "indices trick" repeatedly?

I have a class named memory_region, which is sort of like an untyped gsl::span (i.e. it's essentially a void* and a size_t), which I also use for type erasure. It thus has an as_span() method. With this class, I have a…
einpoklum
  • 118,144
  • 57
  • 340
  • 684
4
votes
1 answer

Using default arguments for std::index_sequence

I have difficulties to understand the following. Why does this code compile template auto foo2(std::index_sequence = std::make_index_sequence()) { constexpr size_t values[] = {N_i...}; …
abraham_hilbert
  • 2,221
  • 1
  • 13
  • 30
3
votes
3 answers

Expand std::vector into parameter pack

I have methods with the following signature: void DoStuff(int i); void DoStuff(int i, k); void DoStuff(int i, int k, int l); I have a method from where I would like to call the DoStuff methods as follows: void CallDoStuff(const std::vector&…
Bob
  • 35
  • 5
3
votes
3 answers

Distribute non-type parameter pack across different template parameter packs

Is there any syntax through which I can distribute a non-type parameter pack across the parameters of a parameter pack of templates, expecting non-type packs (of different sizes)? Since this is pretty confusing I believe that an example may help to…
lightxbulb
  • 1,251
  • 12
  • 29
3
votes
2 answers

Creating a structure with an expanded index sequence

I want to be able to use parameter pack expansion in the initializer list of constructors. Is the best way to achieve this, to endow my class with a parameter pack template argument? Here is an example of what I…
3
votes
2 answers

How to iterate over std::index_sequence

I have this code in my source: template class DimensionPack { public: using Dimensions = std::index_sequence; static const std::size_t total_dimensions = sizeof...(Dims); std::vector
Francis Cugler
  • 7,788
  • 2
  • 28
  • 59
2
votes
2 answers

How to expand multiple index_sequence parameter packs to initialize 2d array in C++?

I'm trying to initialize my Matrix class with std::initializer_lists. I know I can do it with std::index_sequence, but I don't know how to expand them in one statement. This is how I do it: template class Matrix { public: …
sshd
  • 49
  • 1
  • 4
1
vote
1 answer

Automatically generating switch statements at compile time for sparse array indexing

Is there a way to generate compile-time switch statements, for matching indices? For example if I have a sequence 1,5,8 and I want to match it to 0,1,2, is there a way the compiler can generate a function at compile time, which when given 1,5,8…
lightxbulb
  • 1,251
  • 12
  • 29
0
votes
2 answers

Can I calculate Speedup for OpenCL kernels with templates and std::index_sequence?

tldr; How do I implement a for loop that runs a timed function with std::index_sequence? Okay, I'll admit that title is a little cryptic but I was looking at this question: is that possible to have a for loop in compile time with runtime or even…
Jack Benson
  • 107
  • 1
  • 8
0
votes
1 answer

C++ Build error: expected type-specifier before [Variadic templates] class

Here is my code: #include #include #include template inline void DispatchToMethodImpl(const Obj& obj, Method method, Args&& args, …
0
votes
1 answer

How does std::index_sequence_for() exactly work?

I have the following code #include #include #include using namespace std::literals::string_literals; template< typename tupleType, size_t ... inds > void printTupleH(const tupleType& tuple, std::index_sequence
asmmo
  • 6,922
  • 1
  • 11
  • 25
0
votes
1 answer

Recursive aggregate type configured using many template parameters using std::index_sequence

There is a class template: template class Feature { /* Implementation goes here */ }; All the instantiations of Feature<...> are 'collected' here: template
0
votes
3 answers

How Do I Unroll a Variadic Template in a Condition?

I'm looking to unroll a variadic template into separate functions used in the conjunction of an if-statement. Here's an example of what I'm trying to do: template bool bar(const T& param) { return param[I] != 13; } template…
Jonathan Mee
  • 37,899
  • 23
  • 129
  • 288
1
2