variadic-templates

Expand std::vector into parameter pack

北城以北 提交于 2020-01-16 01:15:29
问题 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<int>& vElements) { // What magic is supposed to happen here to make vElements an expandable pack? DoStuff(vElemets...); } Is there any chance to achieve this? Is using std::index_sequence the right way? If yes, could you please provide me a simple example

C++ need a container to store user defined function

≡放荡痞女 提交于 2020-01-15 10:59:29
问题 I am trying to create a interface between user defined function and data. Let's say I need to create a function called MapFun() , input of MapFun() includes user defined function (UDF) handle and UDF inputs. void userFun1(Data data, int in1, int in2){ // user defined function 1; } void userFun2(Data data, int in1, int in2, std::string s){ // user defined function 2; } // ... // apply user function 1 on data MapFun(@userFun1, data, 3, 4); // apply user function 2 on data MapFun(@userFun2, data

Make variadic function which takes arbitary number of void functors

Deadly 提交于 2020-01-15 04:26:08
问题 motivated by my previous question : Make variadic function which takes arbitary functors and returns a tuple of each return value of input functors Now, I want to make a class which just execute each function object which may return void. To the best of my knowledge, I can make something like, class A { public: template <class Func> void operator()(Func func) { func(); } template <class First, class... Funcs> void operator()(First first, Funcs... funcs) { first(); operator()(funcs...); } };

Aliasing a template parameter pack

限于喜欢 提交于 2020-01-15 03:33:09
问题 Pre-Dramatic Hi, maybe this question is a duplicate, but I am relative new to template programming and actually I am not able to find a simple and short solution (-> only finding megabytes of " roxxor-template-magic " which I don't understand) matching my concrete and simple problem and so I feel a little bit lost now. Pre-Information I want to use a "type_container" as template parameter for a class. The container is a simple struct, which should also contain some typedefs for template

Extend templated struct with additional template parameter in C++

谁说我不能喝 提交于 2020-01-14 19:42:46
问题 EXAMPLE template< typename T > struct A { }; template< typename T, typename U > struct A : A<T> { }; int main() { A<double> ad; A<int, int> a; } COMPILE ERROR g++ -std=c++17 -Wall -pedantic -pthread main.cpp && ./a.out main.cpp:9:8: error: redeclared with 2 template parameters struct A : A<T> { ^ main.cpp:4:8: note: previous declaration 'template<class T> struct A' used 1 template parameter struct A { ^ main.cpp: In function 'int main()': main.cpp:16:5: error: expected initializer before 'A'

What is the meaning of “… …” token? i.e. double ellipsis operator on parameter pack

被刻印的时光 ゝ 提交于 2020-01-14 09:06:11
问题 While browsing through gcc's current implementation of new C++11 headers, I stumbled upon "......" token. You can check, that the following code compiles fine [via ideone.com]. template <typename T> struct X { /* ... */ }; template <typename T, typename ... U> struct X<T(U......)> // this line is the important one { /* ... */ }; So, what is the meaning of this token? edit: Looks like SO trimmed "......" in question title to "...", I did really mean "......" . :) 回答1: Every instance of that

how to return the last type of a variadic template?

狂风中的少年 提交于 2020-01-14 07:54:13
问题 For example template<typename... Ts> LastTypeOfTs f(); How to return the last type of a variadic template? 回答1: You could do a template recursion as below: template<typename T, typename... Ts> struct LastTypeOfTs { typedef typename LastTypeOfTs<Ts...>::type type; }; template<typename T> struct LastTypeOfTs<T> { typedef T type; }; template<typename... Ts> typename LastTypeOfTs<Ts...>::type f() { //... } LIVE DEMO 来源: https://stackoverflow.com/questions/26088683/how-to-return-the-last-type-of-a

How to handle unused warnings caused by empty template parameter pack expansions?

若如初见. 提交于 2020-01-14 07:03:14
问题 An issue I keep facing is one where the compiler complains about an unused variable, even though the variable is used, but it's only used inside a parameter pack expansion that happens to be empty for a specific instantiation. For example: template <std::size_t... I> auto func1(std::index_sequence<I...>) { auto var = get_tuple(); return func2(std::get<I>(var)...); } auto a = func1(std::make_index_sequence<0>()); See live example (try changing the tuple at line 4, by adding an int inside <> to

Check a parameter pack for all of type T

大城市里の小女人 提交于 2020-01-12 14:02:45
问题 Jonathan Wakely's answer to the question Type trait to check that all types in a parameter pack are copy constructible gives a simple(ish) way to check if all of the variables expanded in a parameter pack are of the same type - eg: #include <type_traits> namespace detail { enum class enabler {}; } template <bool Condition> using EnableIf = typename std::enable_if<Condition, detail::enabler>::type; template<typename... Conds> struct and_ : std::true_type {}; template<typename Cond, typename...

Can I partially specialize a template with a pattern like foo<T…, int, U…>?

Deadly 提交于 2020-01-11 05:14:28
问题 If this is possible, one can index into a variadic template parameter pack without recursion. However, GCC is refusing to pick up my partial specialization here: template <int I, typename List> struct element_impl; template <typename... TL, int... IL, typename T, int I, typename... TR, int... IR> struct element_impl<I, typelist<pair<TL,IL>..., pair<T,I>, pair<TR,IR>...>> { typedef T type; }; prog.cpp: In instantiation of ' element<0, typelist<int, double, char, float, long int> > ': prog.cpp