Ad
Generate Integer Sequence For Template Parameter Pack
There is a class method template with parameter pack I want to call, defined as:
class C {
template<int ... prp> void function() {}
}
For a given integer N, I need all integers up to N as template arguments for the parameter pack.
constexpr int N = 2;
C c;
c.function<0, 1>();
I have tried using std::integer_sequence, but it can't be used here.
c.function<std::make_integer_sequence<int, N>>();
I also found this answer: Passing std::integer_sequence as template parameter to a meta function Could I pass the function inside the partial specialization, e.g. using std::function? I was not able to use template arguments with std::function.
Additionally, the class has multiple function I would like to call the same way.
c.function1<0, 1>();
c.function2<0, 1>();
There must be a nice way to solve this but I wasn't successful yet. Still trying to understand TMP.
Ad
Answer
You might create helper function:
template <int... Is>
void helper(C& c, std::integer_sequence<int, Is...>)
{
c.function1<Is...>();
c.function2<Is...>();
}
And call it
constexpr int N = 2;
C c;
helper(c, std::make_integer_sequence<int, N>());
Ad
source: stackoverflow.com
Related Questions
- → Comparing two large files are taking over four hours
- → Setting JSON node name to variable value
- → Compiling GLUT using Emscripten
- → Evaluate check box from a scanned image in node.js
- → Find an easy web server framework for mobile game
- → my https C++ code doesn't work on some sites (binance)
- → Error while opening pivx wallet on ubuntu
- → Why sending a POST by AJAX is interpreted by the HTTP Server as OPTIONS and sending by CURL is effectively a PUT?
- → Python reading in one line multiple types for a calculator
- → How do I properly pass an argument to a function
- → Accessing Websql database with Qt
- → Using Mysql C API for c++ codes
- → How do I set constants at run-time in a c++ header file, imported through Cython?
Ad