variadic-templates

Iterate over class inheritances in C++

元气小坏坏 提交于 2021-01-28 14:02:08
问题 Assume I have a some classes architecture (the number of the classes is growing up during the development time), that each class inherit from N classes with the same basic interface. What is the best way (if possible) to create a base function (in the base class OR in the derived class) that will iterate over the inheritances? Target: Avoid developers mistakes and make sure we won't forget to call all the base functions from all of the inheritances & make the code more clear to read and

Multiple parameter packs in a single function?

时光毁灭记忆、已成空白 提交于 2021-01-27 07:31:55
问题 I'm trying to create a function that takes two parameter packs of objects. There are two templated base classes and I'd like to pass instances of derived classes to this function. Consider this example. template <int N> struct First {}; template <int N> struct Second {}; // there are a few of these struct FirstImpl : First<5> {}; struct SecondImpl : Second<7> {}; template <int... firstInts, int... secondInts> void function(float f, First<firstInts> &... first, Second<secondInts> &... second)

Multiple parameter packs in a single function?

て烟熏妆下的殇ゞ 提交于 2021-01-27 07:31:43
问题 I'm trying to create a function that takes two parameter packs of objects. There are two templated base classes and I'd like to pass instances of derived classes to this function. Consider this example. template <int N> struct First {}; template <int N> struct Second {}; // there are a few of these struct FirstImpl : First<5> {}; struct SecondImpl : Second<7> {}; template <int... firstInts, int... secondInts> void function(float f, First<firstInts> &... first, Second<secondInts> &... second)

Template parameter pack deduction when not passed as last parameter

旧时模样 提交于 2021-01-27 04:23:58
问题 Consider the following code: #include <iostream> #include <functional> template<typename... Args> void testfunc(const std::function<void (float, Args..., char)>& func) { } int main(int argc, char* argv[]) { auto func = [](float, int, char) {}; auto sfunc = static_cast<std::function<void (float, int, char)>>(func); testfunc<int>(sfunc); return 0; } I specify the type explicitly because (https://stackoverflow.com/a/40476083): When a parameter pack doesn't appear last in the parameter

Construct tuple by passing the same argument to each element with explicit constructor

天大地大妈咪最大 提交于 2021-01-23 06:51:12
问题 The following works fine on Visual C++ 2015 Update 2. Note that A is non-copyable and A::A is explicit . #include <iostream> #include <tuple> struct A { explicit A(int i) { std::cout << i << " "; } // non-copyable A(const A&) = delete; A& operator=(const A&) = delete; }; template <class... Ts> struct B { std::tuple<Ts...> ts; B(int i) : ts((sizeof(Ts), i)...) { } }; int main() { B<A, A, A, A> b(42); } The goal is to pass the same argument to all tuple elements. It correctly outputs: 42 42 42

Black Magic using Initializer_list and pack expansion

不问归期 提交于 2021-01-22 14:40:19
问题 To expand flexible function parameters, there is a method using std::initializer_list . However I couldn't understand it. Can anyone explain this in an understandable way? template<typename T, typename... Args> auto print(T value, Args... args) { std::cout << value << std::endl; return std::initializer_list<T>{([&] { std::cout << args << std::endl; }(), value)...}; } 回答1: This is a very confused way of doing things, but C++14 requires that we do something similar. I'll explain the limitations

Black Magic using Initializer_list and pack expansion

谁说我不能喝 提交于 2021-01-22 14:35:52
问题 To expand flexible function parameters, there is a method using std::initializer_list . However I couldn't understand it. Can anyone explain this in an understandable way? template<typename T, typename... Args> auto print(T value, Args... args) { std::cout << value << std::endl; return std::initializer_list<T>{([&] { std::cout << args << std::endl; }(), value)...}; } 回答1: This is a very confused way of doing things, but C++14 requires that we do something similar. I'll explain the limitations

Black Magic using Initializer_list and pack expansion

牧云@^-^@ 提交于 2021-01-22 14:33:10
问题 To expand flexible function parameters, there is a method using std::initializer_list . However I couldn't understand it. Can anyone explain this in an understandable way? template<typename T, typename... Args> auto print(T value, Args... args) { std::cout << value << std::endl; return std::initializer_list<T>{([&] { std::cout << args << std::endl; }(), value)...}; } 回答1: This is a very confused way of doing things, but C++14 requires that we do something similar. I'll explain the limitations

Extracting function argument types as a parameter pack

只谈情不闲聊 提交于 2021-01-21 10:34:26
问题 This is a followup question to "unpacking" a tuple to call a matching function pointer, which asked how to provide the values from a std::tuple as arguments to a function in a generic way. A solution given there was the following: template<int ...> struct seq { }; template<int N, int ...S> struct gens : gens<N-1, N-1, S...> { }; template<int ...S> struct gens<0, S...> { typedef seq<S...> type; }; double foo(int x, float y, double z) { return x + y + z; } template <typename... Args> struct

Variadic CRTP Base Class with Additional Template Parameters

余生长醉 提交于 2021-01-07 02:19:19
问题 I have a trait classes which are about to be used in variadic CRTP to extend the features of SmartPointer class. This question is created as the followup of https://stackoverflow.com/a/65373058/5677080 An example trait class: template<typename DERIVED, typename DELETER> class Owning { public: using deleter_type = DELETER; /* ... */ }; Here comes the implemnetation of my SmartPointer which is being extended by variadic CRTP by those trait classes: template<typename T, template<typename> class