variadic-templates

How do I strip a tuple<> back into a variadic template list of types?

孤人 提交于 2019-12-18 03:19:29
问题 Is there a way to strip a std::tuple<T...> in order to get it back to T... ? Example Suppose vct<T...> is a pre-existing variadic class template , using U = std::tuple<int,char,std::string>; using X = vct<int,char,std::string>; using Y = vct< strip<U> >; // should be same as X Notes I know about std::tuple_element, but I need all the elements, in a form that is usable as T... For reference, I have found this question, which is similar, but my needs are somewhat simpler (so I hope there is a

Simulate variadic templates in c#

喜你入骨 提交于 2019-12-18 03:03:14
问题 Is there a well known way for simulating the variadic template feature in c#? For instance, I'd like to write a method that takes a lambda with an arbitrary set of parameters. Here is in pseudo code what I'd like to have: void MyMethod<T1,T2,...,TReturn>(Fun<T1,T2, ..., TReturn> f) { } Thank you 回答1: C# generics are not the same as C++ templates. C++ templates are expanded compiletime and can be used recursively with variadic template arguments. The C++ template expansion is actually Turing

isn't non-type parameter pack that evaluates to “void…” illegal?

左心房为你撑大大i 提交于 2019-12-18 02:41:15
问题 gcc-4.8 accepts this code, but isn't it wrong since the non-type parameter pack is equivalent to void... which is illegal? template <typename T, typename std::enable_if<std::is_integral<T>::value>::type...> void test(T) {} I tried this with clang-3.5 as well which also accepts it. Is this a compiler bug, or am I misunderstanding something? Full test code below, which uses non-type empty parameter packs to simplify enable_if. This is almost the same as what's in Flaming Dangerzone's Remastered

Variadic template templates and perfect forwarding

◇◆丶佛笑我妖孽 提交于 2019-12-17 21:45:53
问题 This question on the object generator pattern got me thinking about ways to automate it. Essentially, I want to automate the creation of functions like std::make_pair , std::bind1st and std::mem_fun so that instead of having to write a different function for each template class type, you could write a single variadic template template function that handles all cases at once. Usage of this function would be like: make<std::pair>(1, 2); // equivalent to std::make_pair(1, 2) make<std::binder2nd>

A timer for arbitrary functions

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-17 20:09:57
问题 I tried to build a function template that can measure the execution time of functions of arbitrary type. Here is what I've tried so far: #include <chrono> #include <iostream> #include <type_traits> #include <utility> // Executes fn with arguments args and returns the time needed // and the result of f if it is not void template <class Fn, class... Args> auto timer(Fn fn, Args... args) -> std::pair<double, decltype(fn(args...))> { static_assert(!std::is_void<decltype(fn(args...))>::value,

A function with variable number of arguments with known types, the c++11 way

北战南征 提交于 2019-12-17 18:35:29
问题 I already know the stdarg.h way to have a function with variable arguments in c++ as discussed here for example. I also know c++11 standard has variadic templates as explained here. But in both of aforementioned schemes we don't know (and we can't force) argument types in compile time afaik. What I'm looking for is to pass variable arguments of known types to a function. I think this can be done because I read about it here: Variadic templates, which can also be used to create functions that

details of std::make_index_sequence and std::index_sequence

烂漫一生 提交于 2019-12-17 10:46:18
问题 I'm enjoying ramping up on variadic templates and have started fiddling about with this new feature. I'm trying to get my head around the implementation details of std::index_sequence 's (used for tuple implementation). I see sample code around there, but I really want a dumbed down step by step explanation of how an std::index_sequence is coded and the meta programming principal in question for each stage. Think really dumbed down :) 回答1: I see sample code around there, but I really want a

C++11 “overloaded lambda” with variadic template and variable capture

不羁岁月 提交于 2019-12-17 09:52:42
问题 I'm investigating a C++11 idiom which might be called "overloaded lambda": http://cpptruths.blogspot.com/2014/05/fun-with-lambdas-c14-style-part-2.html http://martinecker.com/martincodes/lambda-expression-overloading/ Overloading n functions with variadic template seemed very appealing to me but it turned out it didn't work with variable capture: any of [&] [=] [y] [&y] (and [this] etc if in a member function) lead to compilation failure: error: no match for call to '(overload<main(int, char*

variadic template parameter pack expanding for function calls

笑着哭i 提交于 2019-12-17 09:44:42
问题 I am looking for something like that: template< typename T> void func(T t) { } template< typename... Parms> void anyFunc( Parms... p) { func<Parms>(p)... ; //error func(p)... ; //error } If the parameter pack expansion is done inside another function call it works: template< typename T> int some(T t) {} template< typename... Parms> void func(Parms ...p) {} template< typename... Parms> void somemore(Parms... p) { func( some(p)...); } int main() { somemore(1,2,3,4,10,8,7, "Hallo"); } The

Can someone please explain the “indices trick”?

我只是一个虾纸丫 提交于 2019-12-17 09:43:48
问题 I noticed the "indices trick" being mentioned in the context of pretty-printing tuples. It sounded interesting, so I followed the link. Well, that did not go well. I understood the question, but could really not follow what was going on. Why do we even need indices of anything? How do the different functions defined there help us? What is 'Bare'? etc. Can someone give a play-by-play of that thing for the less-than-experts on parameter packs and variadic tuples? 回答1: The problem is: we have a