variadic-templates

Unpacking variadic template parameters into initializer list

狂风中的少年 提交于 2019-12-07 14:31:36
问题 I am currently trying to implement a general initializer to reduce the size of our codebase. At one point however, my code looked like this: template<typename T, typename Arg1, typename Arg2> T* ManageDevice(Arg1 arg1, Arg2 arg2) { auto device = new T{ arg1, arg2 }; // More operations on device return device; } template<typename T, typename Arg1, typename Arg2, typename Arg3> T* ManageDevice(Arg1 arg1, Arg2 arg2, Arg3 arg3) { auto device = new T{ arg1, arg2, arg3 }; // More operations on

Build template from arguments of functions?

对着背影说爱祢 提交于 2019-12-07 12:29:24
问题 template<class... Foos> // N = sizeof...(Foos) template<typename... Args> // M = sizeof...(Args) void split_and_call(Args&&... args) { // Using Python notation here... Foos[0](*args[:a]); // a = arity of Foos[0] Foos[1](*args[a:b]); // b-a = arity of Foos[1] ... Foos[N-1](*args[z:M]); // M-z = arity of Foos[N-1] } Assumptions: All types in Foos are callable All types in Foos are unambiguous Any type in Foos may have an arity of 0 Args is the concatenation of all of the argument types used by

Can a multidimensional array be filled from variadic template?

心已入冬 提交于 2019-12-07 12:01:11
问题 So i have something like that: template<unsigned int W,unsigned int H> class Class { int data[W][H]; Class(const (&_data)[W][H]) { for (int x=0;x<W;x++) for (int y=0;y<H;y++) data[x][y] = _data[x][y]; } template<class... args> Class() { /// black magic } } What could i replace the "black magic", so the second constructor will accept W*H ints? Example: Class<3,2> class1(1,2,3,4,5,6); 回答1: you could use std::initializer_list as a constructor parameter, since your array only is of type int[][] .

c++ non-type parameter pack expansion

白昼怎懂夜的黑 提交于 2019-12-07 09:22:51
问题 I am writing template function that is parametrized by single type, and has variable number of parameters of the same type (not of different types). It should check if first value is among the rest. I wanted to write it like this: #include <unordered_set> template <typename T> static bool value_in(T val, T vals...) { // compiles, but uses only vals[0]: const std::unordered_set<T> allowed {vals}; // error: pack expansion does not contain any unexpanded parameter packs: // const std::unordered

Mixing constructors with fixed parameters and constructors with constructor templates

可紊 提交于 2019-12-07 08:38:50
问题 Is it possible to mix constructors with fixed parameters and constructor templates? My Code: #include <iostream> class Test { public: Test(std::string, int, float) { std::cout << "normal constructor!" << std::endl; } template<typename ... Tn> Test(Tn ... args) { std::cout << "template constructor!" << std::endl; } }; int main() { Test t("Hello World!", 42, 0.07f); return 0; } This gives me "template constructor!". Is there a way, that my normal constructor is called? 回答1: Sure, in the event

How to extract a value from a variadic template parameter pack by index?

我是研究僧i 提交于 2019-12-07 07:29:00
问题 I want to write a function magic_get , which can extract a value from a parameter pack by index, for example: int n = 0; n = magic_get<0>(1, 3, 5, 7); assert(1 == n); n = magic_get<1>(1, 3, 5, 7); assert(3 == n); n = magic_get<2>(1, 3, 5, 7); assert(5 == n); n = magic_get<3>(1, 3, 5, 7); assert(7 == n); How to implement magic_get ? 回答1: template <size_t N, typename... Args> decltype(auto) magic_get(Args&&... as) noexcept { return std::get<N>(std::forward_as_tuple(std::forward<Args>(as)...));

template, well formedness and zero pack length rule

谁说胖子不能爱 提交于 2019-12-07 07:10:05
问题 From the accepted answer of a previous question I've discovered a rule I didn't know about templates and well formedness The program is ill-formed, no diagnostic required, if: [...] every valid specialization of a variadic template requires an empty template parameter pack, or [...] According this rule (if I understand correctly), the following template function is ill-formed template <typename ... Ts> int foo (std::tuple<Ts...> const &) { return std::get<sizeof...(Ts)>(std::tuple<int>{42});

Questions on lambda overloads, type conversions and perfect forwarding

♀尐吖头ヾ 提交于 2019-12-07 06:39:35
问题 This is a question on lambda overload sets and perfect forwarding and somewhat of a followup to a comment. For more context of how this is used see another related question. I have some questions on the below code snippet. Q1: For lambda overloads, I was using overload(Fs...) -> overload<Fs...> from this post, but then in this answer I saw overload(Fs&&...) -> overload<std::decay_t<Fs>...> . In what situations is this difference relevant? Q2: Why would you want to define the identity function

can't understand variadic templates in c++

China☆狼群 提交于 2019-12-07 06:13:45
问题 I was reading about variadic templates and I came across this example. The book mentions that to end the recursion process, the function print() is used. I really can't understand its use. Why does the author use this empty print() function? void print () // can't get why this function is used { } template <typename T, typename... Types> void print (const T& firstArg, const Types&... args) { std::cout << firstArg << std::endl; // print first argument print(args...); // call print() for

Insert/remove type into variadic template list (parameter pack)

陌路散爱 提交于 2019-12-07 05:51:19
问题 What is the best way of implementing index-based insertion and deletion of a type in a variadic template type list (parameter pack)? Desired code/behavior: template<typename...> struct List { /* ... */ }; static_assert(is_same < List<int, char, float>::Insert<int, 0>, List<int, int, char, float> >()); static_assert(is_same < List<int, char, float>::Insert<int, 2>, List<int, char, int, float> >()); static_assert(is_same < List<int, char, float>::Remove<0>, List<char, float> >()); static_assert