variadic-templates

Why can a variadic class template have at most one parameter pack?

孤街浪徒 提交于 2019-12-04 02:27:56
问题 There are moments when wish I could write a class template parameterized by a punctuated list of variadic template parameter packs, e.g. template<typename ...lhs, int Punct, typename ...rhs> struct tuple_pair { std::tuple<lhs...> _lhs; std::tuple<rhs...> _rhs; }; or for that matter: template<int ...lhs, typename Punct, int ...rhs> struct seq_pair { std::integer_sequence<int,lhs...> _lhs; std::integer_sequence<int,rhs...> _rhs; }; These may very well be moments when I am wishing for a grubby

Unpacking parameter packs in template aliases

扶醉桌前 提交于 2019-12-04 02:00:46
I run into a problem with unpacking variadic templates into a template alias. The following code works with Clang 3.4 and GCC 4.8 but fails with GCC 4.9: template <typename T, typename...> using front_type = T; template <typename... Ts> struct foo { using front = front_type<Ts...>; }; GCC 4.9 complains: test.cc:7:37: error: pack expansion argument for non-pack parameter 'T' of alias template 'template<class T, class ...> using front_type = T' using front = front_type<Ts...>; ^ test.cc:1:15: note: declared here template <typename T, typename...> ^ There exists a filed GCC bug ( #59498 ), but is

Checking type of parameter pack using enable_if

纵然是瞬间 提交于 2019-12-04 01:13:01
Since there is a restriction on allowed non-type variadic templates , I am trying to write a function taking an arbitrary number of doubles using enable_if . In essence, I want to do something like: template<typename... T, typename = typename std::enable_if<std::is_convertible<T, double>::value, T>::type> foo(T... t){ /* code here */ } I'm opting to put the enable_if as a default value for an unnamed parameter since my function is actually a constructor and will not have a return value. This would work for a single parameter, but as it's a variadic template T is a parameter pack, and the above

Forwarding arguments to template member function

那年仲夏 提交于 2019-12-04 00:56:07
ideone example I need to forward some predefined arguments plus some user-passed arguments to a member function. #define FWD(xs) ::std::forward<decltype(xs)>(xs) template<class T, class... Ts, class... TArgs> void forwarder(void(T::*fptr)(Ts...), TArgs&&... xs) { T instance; (instance.*fptr)(FWD(xs)..., 0); // ^ // example predefined argument } forwarder(&example::f0, 10, 'a'); forwarder(&example::f1, 10, "hello", 5); This works properly for non-template member functions. The member function pointer passed to forwarder can, however, point to template functions as well. Unfortunately, the

Error with variadiac template: “parameter pack must be expanded”

拟墨画扇 提交于 2019-12-04 00:53:31
Here's a variadic template function that I've written: template<class Container, class Value, class... Args> Value& insert(Container& c, Args&&... args) { c.emplace_back(args); return c.back(); } When I use insert like this I'm getting an error: list<int> lst; int& num = insert<list<int>, int, int>(lst, 4); The error complains about this line in the body of insert : c.emplace_back(args); // <= 'args' : parameter pack must be // expanded in this context What does that mean and how can I fix it? Jarod42 The error is due to the missing ellipsis ( ... ) after args when passing all individual

Two variadic templates for a single function?

倾然丶 夕夏残阳落幕 提交于 2019-12-04 00:43:48
问题 In C++11 is it possible to have two variadic templates for a single function ? If not, is there a trick to write something like that : template <class... Types, class... Args> void f(const std::tuple<Types...>& t, Args&&... args) 回答1: That's perfectly legal: #include <tuple> using namespace std; template <class... Types, class... Args> void f(const std::tuple<Types...>& t, Args&&... args) { // Whatever... } int main() { std::tuple<int, double, bool> t(42, 3.14, false); f(t, "hello", true, 42,

Matching variadic non-type templates

南楼画角 提交于 2019-12-03 23:59:05
Let's say I have two structs, Foo and Bar : template<int...> struct Foo{}; template<unsigned long...> struct Bar{}; I want to create a type trait (call it match_class ) that returns true if I pass two Foo<...> types or two Bar<...> types, but false if I try to mix them: int main() { using f1 = Foo<1, 2, 3>; using f2 = Foo<1>; using b1 = Bar<1, 2, 3>; using b2 = Bar<1>; static_assert(match_class<f1, f2>::value, "Fail"); static_assert(match_class<b1, b2>::value, "Fail"); static_assert(!match_class<f1, b1>::value, "Fail"); } For C++1z (clang 5.0.0 and gcc 8.0.0) it's sufficient to do this ( Demo

Variadic template as template parameter: deduction works with GCC but not with Clang

人盡茶涼 提交于 2019-12-03 22:20:28
While compiling some C++11 code with both GCC 4.7.2 and Clang 3.1, I ran into a problem with Clang not managing to deduce a template argument where GCC succeeds. In a more abstract form, the code looks like this: src/test.cc: struct Element { }; template <typename T> struct FirstContainer { }; template <typename T, typename U = Element> struct SecondContainer { }; template <template <typename> class Container> void processOrdinary(Container<Element> /*elements*/) { } template <template <typename, typename> class Container> void processOrdinary(Container<Element, Element> /*elements*/) { }

variadic template function to concatenate std::vector containers

走远了吗. 提交于 2019-12-03 21:43:19
While learning about template parameter packs, I'm trying to write a clever, simple function to efficiently append two or more std::vector containers together. Below are two initial solutions. Version 1 is elegant but buggy, as it relies on side-effects during the expansion of the parameter pack, and the order of evaluation is undefined. Version 2 works, but relies on a helper function that requires two cases. Yuck. Can you see if you can come up with a simpler solution? (For efficiency, the vector data should not be copied more than once.) #include <vector> #include <iostream> // Append all

How to unpack a variadic template parameter with a numeric sequence?

亡梦爱人 提交于 2019-12-03 20:18:20
How to (or is it possible to) unpack a parameter pack with a numeric sequence? For example, template <typename C, typename... T> C* init_from_tuple(bp::tuple tpl) { return new C{bp::extract<T>("magic"(tpl))...}; // <-- } which the <-- line should expand to return new C{bp::extract<T0>(tpl[0]), bp::extract<T1>(tpl[1]), ..... bp::extract<Tn>(tpl[n])}; where n == sizeof...(T) - 1 . The purpose is to create a __init__ function for Boost.Python which accepts a tuple with predefined types. Actually, it is possible for the unpack operations to target two different packs of parameters at once (I think