variadic-templates

How to check that all types in variadic template are convertible to size_t?

爷,独闯天下 提交于 2019-12-11 11:55:55
问题 How can I check that all types in a variadic template declaration can be converted to size_t: // instantiate only if extents params are all convertible to size_t template<typename T, size_t N> template<typename... E> Array<T,N>::Array(E... extents) { constexpr size_t n = sizeof...(extents); static_assert(n == N, "Dimensions do not match"); // code for handling variadic template parameters corresponding to dimension sizes } With the following usage: Array<double, 2> a(5,6); // OK 2-D array of

Member function pointer wrapper using variadic template

爱⌒轻易说出口 提交于 2019-12-11 10:35:44
问题 I'm currently trying to compile my code using Visual C++ 2013 and want to take the advantage of variadic templates. I have several classes which wrap the function pointer - several versions for different number of arguments. The wrapper for a member function with one argument is the following: template <typename T, T> struct proxy; template <typename T, typename R, typename Arg0, R(T::*mf)(Arg0)> struct proxy<R(T::*)(Arg0), mf> { proxy(T& host) : m_Host(host) {} template <typename Arg0> R

Make variadic function which takes arbitary functors and returns a tuple of each return value of input functors

a 夏天 提交于 2019-12-11 10:10:14
问题 I want to make a function object which takes arbitrary function objects and returns a tuple which stores the return value of each function object. To achieve this goal, I made a class A class A { private: template <class Ret, class Func> auto impl(Ret ret, Func func) -> decltype(tuple_cat(ret, make_tuple(func()))) { return tuple_cat(ret, make_tuple(func())); } template <class Ret, class First, class... Funcs> auto impl(Ret ret, First first, Funcs... funcs) -> decltype(impl(tuple_cat(ret, make

Reference counting with a generic intrusive pointer client

亡梦爱人 提交于 2019-12-11 08:46:55
问题 Introduction Peter Weinhart describes how to design a generic intrusive_ptr base class using CRTP, which may be used as follows: class foo : intrusive_base<foo> { // foo-specific code. }; This approach imposes the constraint that all foo objects carry a reference counter. Assume that we keep foo sometimes by value and only want to pay the price of the reference counter when we have a pointer. For example, sometimes we would like to create foo instances and just move them around, and sometimes

getting original type of this object

随声附和 提交于 2019-12-11 08:00:09
问题 I'm trying to use metaprogramming to prevent duplicate code in a parent-child structure. I got it working up to a certain point. The code shown at the bottom compilers and runt correctly, but some relations ( /*Tree_tag,*/ and /*Parasite_tag*/ ) are commented out. If uncommented, MSVS2017 shows error C2664: 'void Obj<std::tuple<Human_tag>,std::tuple<>>::removeParent(const Obj<std::tuple<>,std::tuple<Tree_tag,Dog_tag>> *const )': cannot convert argument 1 from 'Obj<std::tuple<>,std::tuple<Dog

Cast specific types in variadic argument

时光总嘲笑我的痴心妄想 提交于 2019-12-11 07:35:11
问题 I have a template function that accepts variadic arguments. template<typename... Params> void foo(Params... p); I want to find all occurences of a given type ( const char* ) in Params to replace them with another type, that these values can be cast to (my own Path class with constructor Path(const char*) ). The idea is to have something like template<typename... Params> void foo(Params... p) { bar<convertCharPointerToPath<Params>...>(p...); } How can this conversion be done? 回答1: If you want

Variadic template parameter pack to accept only unsigned ints or size_t as its type

本小妞迷上赌 提交于 2019-12-11 07:31:17
问题 I'm trying to use a set of template classes with a variadic parameter. I have several options ahead of me that I could choose from. Before any of my templates are declared or defined I currently have these prototypes: I'm familiar with templates but I haven't had much experience with variadic types when working with templates so the syntax does get a little confusing to me at times. Being that they are all empty shells they do currently compile. template<typename ClassType, typename... Args>

Instantiating a variadic member function template of a class template

折月煮酒 提交于 2019-12-11 07:11:32
问题 How can I instantiate a variadic member function template of a class template in separate .cpp file? Say, given an above class template in a set of files: a.hpp with definition of interface, a_impl.hpp with implementation and a.cpp with instantiation, - which includes each previous in the chain sequentially, but only the first is visible to the user of the class (as opposed to the developer). Especially interested in case of an empty parameter pack. 回答1: template <class A> struct AA {

Variadic template function overloading

岁酱吖の 提交于 2019-12-11 05:57:38
问题 I have a class with a variadic template member function (foo) like below. The idea is to skip all doubles in the parameter and allocate an object with user provided arguments. template <class T> class Var { public: template <typename U, typename ...Args> int foo(int index, Args... args) { T* p = new U(args...); // save in an array at index 'index' } template <typename U, typename ...Args> int foo (double index, Args... args) { // do something with index and skip it return foo<U>(args...); } }

Function wrapper via (function object) class (variadic) template

两盒软妹~` 提交于 2019-12-11 05:54:50
问题 C++ I'm trying to implement a function wrapper via a (function object) class (variadic) template. The class has as its only data member a function pointer that is initialized by or assigned the function pointer it is wrapping. The parametrized constructor takes a function pointer and initializes the member by it. The operator() method takes argument(s) (or none) and calls the wrapped function with them. At least that's the idea. I get many errors, which I mark with comments. VC11 (with the