variadic-templates

Initialzing and accessing members of a variadic class template

女生的网名这么多〃 提交于 2019-12-14 03:46:18
问题 I'm experimenting with variadic templates and would like to know if they can be used in order to generalze(?) class templates such as template<typename T1, typename T2 , typename T4, typename T4> struct Foo { T1 &m_member1; T2 &m_member2; T3 &m_member3; T4 &m_member4; }; Also, I'd like to be able to initialize all the members by chaining constructors. This is how far I've gotten: template<typename... Types> struct Foo; template<typename T , typename... Types> struct Foo<T, Types ...> : public

Function template parameter pack not at the end of the parameter list

心已入冬 提交于 2019-12-14 00:45:50
问题 The following code compiles and runs ok. void foo() { } template <typename T, typename... Args> void foo(T x, Args... args) { cout << x << endl; foo(args...); } // inside main() foo(1,1,1); This other code does not compile: void foo() { } template <typename... Args, typename T> void foo(Args... args, T x) { foo(args...); cout << x << endl; } // inside main() foo(1,1,1); The compiler says that there is no matching function for call to foo(1,1,1) and says that foo(Args... args, T x) is a

Reference parameter being copied in variadic template

回眸只為那壹抹淺笑 提交于 2019-12-13 18:30:46
问题 I have an Event class that stores a set of tuples of weak_ptr (the observer) to a function that gets executed when the event is "fired". The function type is: void(int &) in the example. That is to say, I want to fire the event passing a reference to a value, to have the observer change that value and to verify that the value was changed back in the observed object. The event implementation is variadic by the way, which may complicate the issue (at least the code). At the moment this is

Difference between references and values in deduction guides

会有一股神秘感。 提交于 2019-12-13 14:19:32
问题 Considering type A : template <typename T, size_t N> struct A { T arr[N]; }; Is there any difference between C++17 user-defined deduction guides template <typename T, typename ... Ts> A(const T&, const Ts& ...) -> A<T, 1 + sizeof...(Ts)>; and template <typename T, typename ... Ts> A(T, Ts ...) -> A<T, 1 + sizeof...(Ts)>; ? Or, in other words is there any difference between const references and values in deduction guides? Please note that the question is not about template function type

Obtaining all subpacks from a pack

二次信任 提交于 2019-12-13 11:47:15
问题 PowerSet<Pack<Types...>>::type is to give a pack consisting of packs formed by all subsets of Types... (for now assume the static assertion that every type in Types... are distinct). For example, PowerSet<Pack<int, char, double>>::type is to be Pack<Pack<>, Pack<int>, Pack<char>, Pack<double>, Pack<int, char>, Pack<int, double>, Pack<char, double>, Pack<int, char, double>> Now, I've solved this exercise and tested it, but my solution is very long and would like to hear some more elegant ideas

variadic function template without formal parameters

我是研究僧i 提交于 2019-12-13 11:34:59
问题 This is what I'm trying to do: // base case void f() {} template <typename T, typename... Ts> void f() { // do something with T f<Ts...>(); } int main() { f<int, float, char>(); return 0; } It doesn't compile: prog.cpp: In instantiation of ‘void f() [with T = char; Ts = {}]’: prog.cpp:6:5: recursively required from ‘void f() [with T = float; Ts = {char}]’ prog.cpp:6:5: required from ‘void f() [with T = int; Ts = {float, char}]’ prog.cpp:10:25: required from here prog.cpp:6:5: error: no

Turning a BOUNDED std::list<class> of parameters into a type std::tuple<class,class,class> tup<classObj1, classObj2,classObj2>

老子叫甜甜 提交于 2019-12-13 10:11:05
问题 I have a class class C { public: C() {} private: int timesTriggered_; std::map<std::string, std::tuple<std::string, int, int, int, int, int>> mapST; std::vector<std::string> sv; }; and some objects of this type: C c1; C c2; C c3; I have a map indexed by strings of class C std::map<std::string, std::list<C>> cMap; I add some objects of class C into a map cMap["1"].push_back(c1); cMap["2"].push_back(c2); cMap["3"].push_back(c3); I need a function that when passed the std::list will return (for

Is it possible to create a completely arbitrary private member tuple in a C++11 variadic class constructor?

大兔子大兔子 提交于 2019-12-13 05:50:22
问题 My apologies if this has been asked before - searched with no definite answer, and I'm beginning to wonder if it is even possible. I am trying to learn C++11 and have run into trouble with variadic templates. I think I grasp (finally) the concept of variadic function parameters, and why/how recursion is used to unwrap and process them, but am having trouble with a (I think) similar concept in class constructors. Suppose I want to create a variadic template class that has a mixed-type

Parsing std::vector of std::strings into std::tuple of arbitrary types

我们两清 提交于 2019-12-13 04:39:26
问题 I'm writing a command-line interpreter. In the old C++03 days you had to declare a fixed prototype and then parse arguments inside it. But in C++11 we have variadic templates, so I wanted to write the code that will work with any function prototype and will automatically parse all arguments using std::stringstream. So far, I got the following code: #include <iostream> #include <sstream> #include <string> #include <vector> #include <tuple> #include <functional> template <typename... Args>

Is there a container that can store items of different types?

ε祈祈猫儿з 提交于 2019-12-13 04:26:09
问题 Storing items of the same type is trivial, but I need a container that can store items of different types. Here's an example showing what I'd like to do: Class C { }; C c1; C c2; C c3; std::tuple<C> tup1(c1); std::tuple<C, C> tup2(c1, c2); std::tuple<C, C, C> tup3(c1, c2, c3); container_type container(tup1, tup2, tup3); Is there any container that works in this way? If not, is there a way to create one? I'd like something that overloads operator[] for fast random access. 回答1: It won't work.