variadic-templates

Issues applying std::bind recursively on a std::function

寵の児 提交于 2019-12-21 04:52:28
问题 Given a function f(x, y, z) we can bind x to 0, getting a function g(y, z) == f(0, y, z) . We can continue doing this and get h() = f(0, 1, 2) . In C++ syntax that would be #include <functional> #include <iostream> void foo(int a, long b, short c) { std::cout << a << b << c << std::endl; } int main() { std::function<void(int, long, short)> bar1 = foo; std::function<void(long, short)> bar2 = std::bind(bar1, 0, std::placeholders::_1, std::placeholders::_2); std::function<void(short)> bar3 = std

Why template with only valid empty variadic pack ill formed?

a 夏天 提交于 2019-12-21 04:06:47
问题 What is the rationale of temp.res#8.3 (8) The validity of a template may be checked prior to any instantiation. [ Note: Knowing which names are type names allows the syntax of every template to be checked in this way. — end note ] The program is ill-formed, no diagnostic required, if: [..] (8.3) every valid specialization of a variadic template requires an empty template parameter pack, or That rule disallows trick as following to force template deduction as: template <typename ...Ts,

binding member functions in a variadic fashion

a 夏天 提交于 2019-12-21 03:43:23
问题 I have a member function with a variable number of parameters, stored in a std::function , and I want to bind the instance and get an independent function object. template <class T, class R, class... Args> void connect(const T& t, std::function<R(const T&, Args...)> f) { std::function<R(Args...)> = /* bind the instance c into the function? */ } // ... Class c; connect(c, &Class::foo); For a fixed number of arguments I'd use std::bind , but I don't see how to do this for variadic parameters.

Easiest way to get the N-th argument of a variadic templated class?

我的未来我决定 提交于 2019-12-20 18:42:17
问题 I wonder what is the easiest and more common way to get the N-th parameter of a variadic templated class at compile-time (The returned value has to be as a static const for the compiler in order to do some optimizations). Here is the form of my templated class : template<unsigned int... T> MyClass { // Compile-time function to get the N-th value of the variadic template ? }; Thank you very much. EDIT : As MyClass will contain more than 200 functions, I can't specialize it. But I can

Easiest way to get the N-th argument of a variadic templated class?

与世无争的帅哥 提交于 2019-12-20 18:40:12
问题 I wonder what is the easiest and more common way to get the N-th parameter of a variadic templated class at compile-time (The returned value has to be as a static const for the compiler in order to do some optimizations). Here is the form of my templated class : template<unsigned int... T> MyClass { // Compile-time function to get the N-th value of the variadic template ? }; Thank you very much. EDIT : As MyClass will contain more than 200 functions, I can't specialize it. But I can

Is it possible to write a generic variadic zipWith in C++?

廉价感情. 提交于 2019-12-20 15:24:15
问题 I want a generic zipWith function in C++ of variable arity. I have two problems. The first is that I cannot determine the type of the function pointer passed to zipWith. It must be of the same arity as the number of vectors passed to zipWith and it must accept references to the vectors' element types respectively. The second is that I have no idea how to walk these vectors in parallel to build an argument list, call func(), and bail once the shortest vector is exhausted. template <typename R,

Variadic templates and switch statement?

那年仲夏 提交于 2019-12-20 09:16:37
问题 I have the following function which can take N arguments of different types, and forwards them to N functions templated on each individual type, in this manner (example with two arguments): template <typename T1, typename T2> bool func(int& counter, T1 x1, T2 x2) { switch (counter) { case 0: if (func2<T1>(x1)) { counter++; return true; } else { return false; } case 1: if (func2<T2>(x2)) { counter++; return true; } else { return false; } default: return true; } } I want to write this function

Can I expand a parameters pack and define an arguments list with it?

陌路散爱 提交于 2019-12-20 05:02:41
问题 From [temp.variadic] (working draft) it seemed to me that a parameters pack can be expanded while defining an arguments list of another template class or function. Consider the following class: template<typename... T> struct S { template<T... I> void m() {} }; int main() { S<int, char> s; // ... } The intent is to capture the types used to specialize the template class S and use them to define an arguments list of non-type parameters for the member method m ( T is limited to a few types, of

Apply functions belonging to other classes through variadic templates

北城以北 提交于 2019-12-20 04:16:07
问题 Say I have three classes, ClassA , ClassB , and ClassC . And all three of these classes have a function called updateValue(int) . And then I have controller class called, Controller . Who's constructor is templated like the following: class Controller { public: template <class...Classes> Controller(Classes & ...classes); // maybe initialize a tuple? void setValues(int val) { // unpack whatever data structure and call each classes updateValue(int) } private: std::tuple<Classes...classes>

What is a good alternative to this C++17 fold expression in C++14?

余生长醉 提交于 2019-12-19 21:47:46
问题 Here is a nice, succinct fold expression based lambda in C++17: #include <cstdint> using ::std::uint64_t; constexpr auto sumsquares = [](auto... n) { return ((n * n) + ...); }; // I want this to work. uint64_t foo(uint64_t x, uint64_t y, uint64_t z) { return sumsquares(x, y, z); } // And this too double bar(uint64_t x, double y) { return sumsquares(x, y); } I have this code I've written to do something similar in C++14, but it seems a lot more verbose and confusing than it should be. I'm