variadic-templates

Can a parameter pack be captured implicitly within C++11 lambdas?

☆樱花仙子☆ 提交于 2019-12-06 18:26:21
问题 Does anyone know if the following implicit capture of 'ts' is well-formed: template<class ... Ts> void bar(Ts ... ts) { } template<class ... Ts> int foo(Ts ... ts) { auto L = [=] () { bar(ts...); }; L(); return 0; } int g = foo(1, 2, 3); Does the standard clearly state anywhere that this should not be well formed? 回答1: 14.5.3/6: The instantiation of a pack expansion that is not a sizeof... expression produces a list E1, E2, ..., EN , where N is the number of elements in the pack expansion

Permutation P(N,R) of types in compile time

≡放荡痞女 提交于 2019-12-06 13:37:42
问题 I've written a working code for computing P(N,R) for packs when the pack has all different types, e.g. PermutationN<2, P<int, char, bool>> is to be P< P<int, char>, P<int, bool>, P<char, int>, P<char, bool>, P<bool, int>, P<bool, char> > But when there are repeat elements, I'm getting the wrong results. For example, PermutationN<2, P<int, int, char>> should be P< P<int, int>, P<int, char>, P<char, int> > Here is my working code for when all the types are different. I'm stuck on how to adapt

Why does this variadic template specialization definition not compile?

笑着哭i 提交于 2019-12-06 13:23:11
Using gcc 4.7.3, I get the following error prog.cpp: In function ‘int main()’: prog.cpp:27:63: error: ‘Erase >::Result’ has not been declared with this code : template <typename... List> struct TypeList { enum { Length = sizeof...(List) }; }; template <typename ToErase, typename... List> struct Erase; template <typename ToErase> struct Erase<ToErase, TypeList<>> { typedef TypeList<> Result; }; template <typename ToErase, typename... Head, typename... Tail> struct Erase<ToErase, TypeList<Head..., ToErase, Tail...>> { typedef TypeList<Head..., Tail...> Result; }; int main() { static_assert(Erase

Get the subset of a parameter pack based on a given set of indices

做~自己de王妃 提交于 2019-12-06 12:06:07
Okay, this is a really difficult one. I want to be able to get the subset of a parameter pack by choosing the parameter types at a given set of valid indices and then use that parameter pack as the argument list of a function. IE: template <size_t... indices_t> void func(pack_subset<indices_t..., args_t...>); //incorrect syntax, //not sure how else to write this //args_t is predetermined, this //function is a static member of //a variadic class template //For a parameter pack <float, double> and index set 0 void func(float); // <- result of template I can get the type of a single index, but a

How would I 'generate variadic parameters'?

半城伤御伤魂 提交于 2019-12-06 11:43:25
问题 I need a way to pass a variable amount of parameters to a function in this circumstance: template<typename ...T> struct Lunch { Lunch(T...){} }; template<typename T> T CheckLuaValue(lua_State* luaState,int index) { //Do Stuff return value; } template <class MemberType, typename ReturnType, typename... Params> struct MemberFunctionWrapper <ReturnType (MemberType::*) (Params...)> { static int CFunctionWrapper (lua_State* luaState) { ReturnType (MemberType::*)(Params...) functionPointer =

variadic multidimensional array

自作多情 提交于 2019-12-06 10:04:56
问题 Could I call a multidimensional array like func(0,0,0); //=> if I know it's dimension on the run time. func(0,0,0,0,0,0,0,0,0,0,0); //=> if I know it's dimension on the run time. through help of the variadic templates instead of: data[0][0][0]; data[0][0][0][0][0][0][0][0][0][0][0]; 回答1: This should work but instead of doing data[1][2][3] you will have to use indexed(data,1,2,3) It works for plain arrays as well as std::arrays . You can extend it std::vector just by duplicating the

C++ variadic template with non-type parameters of different types

孤街醉人 提交于 2019-12-06 09:49:16
问题 I would like to define a class which takes a list of non-type parameters, potentially of different types. For example, the following should be valid: Test<3, 4.5, 6> t; If all parameters had the same type, say int , I could use the following definition: template<int... args> class Test { // ... }; To be more specific, in my specific use case there is a second class Base with a number of members, and I would like to pass Base member pointers. Test<&Base::a, &Base::b> If Base::a and Base::b

Can't add perfect forwarding to wrapper function

怎甘沉沦 提交于 2019-12-06 08:48:34
问题 While answering this question I wrote this working code, wrapping function passed in template arguments: template<typename Fn, Fn fn, typename... Args> auto wrapper(Args... args)->decltype(fn(args...)){ return fn(args...); } #define WRAPPER(FUNC) wrapper<decltype(&FUNC), &FUNC> Example usage (I use this code for testing): int min(int a, int b){ return (a<b)?a:b; } #include<iostream> using std::cout; int main(){ cout<<WRAPPER(min)(10, 20)<<'\n'; } Two people told me to use perfect forwarding .

int a[] { (functioncall(a1, a2), 0)…}; (void(a)); What does this syntax do/mean?

喜欢而已 提交于 2019-12-06 07:28:30
I came across this post variadic template function to concatenate std::vector containers suggesting the use of the following syntax: template<typename T> void append_to_vector(std::vector<T>& v1, const std::vector<T>& v2) { std::cout << v2[0] << std::endl; for (auto& e : v2) v1.push_back(e); } template<typename T, typename... A> std::vector<T> concat_version3(std::vector<T> v1, const A&... vr) { int unpack[] { (append_to_vector(v1, vr), 1)... }; (void(unpack)); return v1; } I started playing around with it to understand how it worked, since I haven't seen this: int unpack[] { (append_to_vector

boost::spirit::qi::grammar and variadic templates

与世无争的帅哥 提交于 2019-12-06 06:15:16
I'm facing with an issue in defining a grammar with variadic templates. I started by defining some simple grammars contained into some struct (e.g. Latitude, Longitude) as follows: #include <boost/fusion/include/adapt_struct.hpp> #include <boost/spirit/include/qi.hpp> #include <boost/variant.hpp> #include <iostream> #include <string> using namespace boost::spirit; template <class Attribute> using command_rule = qi::rule<std::string::iterator, Attribute, ascii::space_type>; template <class Attribute> using command_grammar = qi::grammar<std::string::iterator, Attribute, ascii::space_type>;