variadic-templates

template function with corresponding parameters to subset of tuple types

大兔子大兔子 提交于 2019-12-05 20:16:30
问题 I would like to write function as this find : multi_set<int, string, double, myType> m; //vector of tuples m.insert(/*some data*/); m.find<1,2>("something",2.123); Or m.find<0,3>(1,instanceOfMyType); m.find<1>("somethingelse"); Where find can be parametrized corresponding to any subset of tuple parameters. My code so far: template <typename ... T> class multi_set{ typedef tuple < T... > Tuple; vector<tuple<T...>> data = vector<tuple<T...>>(); public: void insert(T... t){ data.push_back(tuple

Build template from arguments of functions?

橙三吉。 提交于 2019-12-05 18:14:07
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 Foos Can this be done with just Foos and without Args ? I'm actually not sure how to do it even if I

What is wrong with this variadic templates example?

拈花ヽ惹草 提交于 2019-12-05 17:44:38
The base class is : #include <memory> namespace cb{ template< typename R, typename ... Args > class CallbackBase { public: typedef std::shared_ptr< CallbackBase< R, Args... > > CallbackPtr; virtual ~CallbackBase() { } virtual R Call( Args ... args) = 0; }; } // namespace cb Derived class is this : namespace cb{ template< typename R, typename ... Args > class FunctionCallback : public CallbackBase< R, Args... > { public: typedef R (*funccb)(Args...); FunctionCallback( funccb cb_ ) : CallbackBase< R, Args... >(), cb( cb_ ) { } virtual ~FunctionCallback() { } virtual R Call(Args... args) { return

How to make variadic template class method take function pointer as argument with type derived from function template?

℡╲_俬逩灬. 提交于 2019-12-05 16:43:43
Sorry the title is a mouthful. I'm working on an array class similar to the one discussed here . I want to define a "map" function that takes a user-defined function and applies it to each element of the array. For the purposes of type-checking, I'd like to define it such that the user-specified function must take the same number of arguments as are passed to the map function, so that double f(double a, double b) { return a + b; } Array<double,2> x, y, z; x.map(f, y, z); will compile but double g(double a, double b, double c) { return a + b + c; } Array<double,2> x, y, z;. x.map(g, y, z); won

Specialization of variadic constructor template of class template

巧了我就是萌 提交于 2019-12-05 15:37:17
Here's a class with variadic constructor and it's specializations for copy and move from a temporary. template<class Obj> class wrapper { protected: Obj _Data; public: wrapper(const wrapper<Obj>& w): _Data(w._Data) {} wrapper(wrapper<Obj>&& w): _Data(std::forward<Obj>(w._Data)) {} template<class ...Args> wrapper(Args&&... args): _Data(std::forward<Args>(args)...) {} inline Obj& operator()() { return _Data; } virtual ~wrapper() {} }; When I use one of specializations like this wrapper<int> w1(9); wrapper<int> w2(w1); I'm getting the error: type of w1 is deduced as int . Output from VS2012:

Apply function on each element in parameter pack

Deadly 提交于 2019-12-05 15:20:56
问题 I have the following template function with specialization: // Pass the argument through ... template<typename T, typename U=T> U convert(T&& t) { return std::forward<T>(t); } // ... but convert std::strings const char* convert(std::string s) { return s.c_str(); } If I then have a variadic template function like: template<typename ... Args> void doSomething(Args ... args) { // Convert parameter pack using convert function above // and call any other variadic templated function with // the

Checking type of parameter pack using enable_if

混江龙づ霸主 提交于 2019-12-05 14:56:33
问题 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

C++ Vector wrapper around variadic function

こ雲淡風輕ζ 提交于 2019-12-05 14:47:31
I have an api that looks like this: template<typename... Args> Widget::Widget(std::string format_str, Args&&... args); How would you call this method if you have a vector of strings for 'args', i.e. the args length isn't known at compile time? What would the implementation of a wrapper function look like that would convert this to something like this? template<typename... Args> Widget::WrapperWidget(std::string format_str, vector<string>); Following may help: #if 1 // Not in C++11 #include <cstdint> template <std::size_t ...> struct index_sequence {}; template <std::size_t I, std::size_t ...Is

Use a templated variadic template parameter as specialized parameter

别说谁变了你拦得住时间么 提交于 2019-12-05 14:37:14
My title might be wrong - if so, please do correct me, but at some point its hard for me to keep track of what meta thingy I'm actually trying to achieve ;) I have a class function template like this: template<template<typename...> class MapType> Expression Expression::substitute(MapType<std::string, Expression> const& identifierToExpressionMap) const { return SubstitutionVisitor<MapType>(identifierToExpressionMap).substitute(something); } The important part is the MapType. The idea is to allow either std::map or std::unordered_map to be plugged in at will. With GCC and Clang, this works, but

C++ variadic template function parameter with default value

夙愿已清 提交于 2019-12-05 13:20:22
问题 I have a function which takes one parameter with a default value. Now I also want it to take a variable number of parameters and forward them to some other function. Function parameters with default value have to be last, so... can I put that parameter after the variadic pack and the compiler will detect whether I'm supplying it or not when calling the function? (Assuming the pack doesn't contain the type of that one last parameter. If necessary, we can assume that, because that type is