variadic-templates

For every template type an argument of a set type

。_饼干妹妹 提交于 2019-12-04 06:23:42
Say I have a variadic template class. How do I create a function such that it's arguments are of a set type, for example int , with the number of arguments being equal to the number of template types? template <typename... Types> class Test { public: void Func(???); // I don't know how to declare such a function } Test<string, bool, long> myTest; // Three types myTest.Func(905, 36, 123315); // Three arguments, but always of type int. In the end, the goal of the function is to return a tuple of the provided ints. For simplicity I showed the function to be void in the example code. template

Parameters after parameter pack in function [duplicate]

坚强是说给别人听的谎言 提交于 2019-12-04 05:28:54
问题 This question already has answers here : Variadic function template with pack expansion not in last parameter (4 answers) Closed 2 years ago . I was able to find one question on SO that seems to be asking the same or similar question that I am, but there are no answers :-( I'd like to place a non-template parameter after a parameter pack. I'm not too familiar on the C++ standard specification for variadic templates / parameter packs, but my common sense assumption tells me that the right-most

C++ function caller wrapper using variadic pack types expansion

岁酱吖の 提交于 2019-12-04 05:09:36
问题 I am bound to some APIs and I am tied to some function signatures like here: static bool WrapperFunction(JSContext *cx, unsigned argc, JS::Value *vp) I try to wrap objects and functions to use in javascript under SpiderMonkey. To integrate some C API, must be implemented wrappers for object data and wrapper methods for some object. My solution lead me to the following logic of the wrapper, in order to be able to call methods with several arguments, but I don't know how to achieve it: template

Variadic template parameters of one specific type

◇◆丶佛笑我妖孽 提交于 2019-12-04 04:19:31
Why there is no specific type allowed in a variadic template pack? template< typename T > class Foo { public: template< typename... Values > void bar( Values... values ) { } template< T... values > <-- syntax error void bar( T... values ) { } template< int... values > <-- syntax error void bar( int... values ) { } }; Whats the rationale in not allowing this? Are there proposals for this? Note: alternatives would be std::initializer_list< T > without narrowing of types and the { } -brace-syntax a (ugly) recursive trait that checks all types seperately: see here It IS allowed, actually, you're

How to pass std::function with different parameters to same function

心已入冬 提交于 2019-12-04 03:27:46
I have three functions I'm looking to merge together. Each one takes an std::function as the first parameter, then executes it within a try / catch block. The issue is, there are three different types of functions. Functions with no parameters, those with one integer parameter, and those with two integer parameters. The ones with integer parameters also have their corresponding parameters passed through the original function. As one can see, each of the functions are nearly identical, so it would be nice if I could merge them all together. However, I'm unsure of anyway to setup a parameter

parameter packs not expanded with ‘…' — another variadic template bug with gcc?

痴心易碎 提交于 2019-12-04 03:16:31
问题 gcc's treatment of variadic templates is well known to be patchy (see for example this and this), but I wonder whether the following bug is already known (I cannot find it at bugzilla) or whether it is indeed a bug. Essentially, gcc (4.8.1) fails to expand a parameter pack inside a lambda: #include <vector> #include <algorithm> #include <type_traits> template<typename T, typename F, typename... X> void bar(std::vector<T> const&c, F const&f, X&&... x) { std:for_each(c.begin(),c.end(),[&](const

Are checked guard parameter packs cause of ill-formed programs in case of specializations?

我的梦境 提交于 2019-12-04 03:05:32
This is a follow-up on this question. Consider the following code: #include <type_traits> template<typename T, typename... P, typename U = std::enable_if_t<std::is_integral<T>::value>> void f() { static_assert(sizeof...(P) == 0, "!"); } int main() { f<int>(); } It compiles, but according to [temp.res]/8 it is ill-formed, no diagnostic required because of: every valid specialization of a variadic template requires an empty template parameter pack Now consider this slightly different example: #include <type_traits> template<typename T, typename... P, typename U = std::enable_if_t<std::is

Apply function on each element in parameter pack

南楼画角 提交于 2019-12-04 02:51:40
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 converted args. } Is there any way to convert the parameter pack using the convert function as in the

C++ Variadic Function Templates of Known Type

北城余情 提交于 2019-12-04 02:51:36
I'm currently trying to get my head around some of the things I can do with variadic template support. Let's say I have a function like this - template <typename ... Args> void foo(Args ... a) { int len = sizeof...(tail); int vals[] = {a...}; /* Rest of function */ } /* Elsewhere */ foo(1, 2, 3, 4); This code works because I assume beforehand that the arguments will be integers, but obviously will fail if I provide something else. If I know that the parameter packs will contain a particular type in advance, is there some way that I can do without the templating and have something like - void

template function with corresponding parameters to subset of tuple types

余生长醉 提交于 2019-12-04 02:39:52
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<T...>(t...)); } template<size_t ... Pos> void find(???){ // then I would like to use those params to