variadic-templates

How to extract lambda's Return Type and Variadic Parameters Pack back from general template<typename T>

烈酒焚心 提交于 2019-12-06 04:43:21
问题 I want to create a templated class or function, that receives a lambda, and puts it internally in std::function<> Lambda could have any number of input parameters [](int a, float b, ...) std::function<> should correspond to the lambda's operator()'s type template <typename T> void getLambda(T t) { // typedef lambda_traits::ret_type RetType; ?? // typedef lambda_traits::param_tuple --> somehow back to parameter pack Args... std::function<RetType(Args...)> fun(t); } int main() { int x = 0;

Pushing and popping the first element of a std::tuple

China☆狼群 提交于 2019-12-06 04:07:18
I am writing a function in C++ with a variable number of arguments (and different types) in this way template<typename ...Ts> void myFunction(Ts ...args) { //create std::tuple to access and manipulate single elements of the pack auto myTuple = std::make_tuple(args...); //do stuff return; } What i would like to do, but I don't know how, is to push and pop elements from the tuple, in particular the first element... something like //remove the first element of the tuple thereby decreasing its size by one myTuple.pop_front() //add addThis as the first element of the tuple thereby increasing its

Using non-const expression as template parameter

非 Y 不嫁゛ 提交于 2019-12-06 02:35:43
This is a follow up on How do I get the argument types of a function pointer in a variadic template class? I have this struct to access the arguments of a variadic template: template<typename T> struct function_traits; template<typename R, typename ...Args> struct function_traits<std::function<R(Args...)>> { static const size_t nargs = sizeof...(Args); typedef R result_type; template <size_t i> struct arg { typedef typename std::tuple_element<i, std::tuple<Args...>>::type type; }; }; And I access the type of an argument of Args with typedef function<void(Args...)> fun; std::cout << std::is

sizeof variadic template (sum of sizeof of all elements)

时光毁灭记忆、已成空白 提交于 2019-12-06 02:09:41
问题 Considering the following function : template<typename... List> inline unsigned int myFunction(const List&... list) { return /* SOMETHING */; } What is the most simple thing to put instead of /* SOMETHING */ in order to return the sum of sizeof all arguments ? For example myFunction(int, char, double) = 4+1+8 = 13 回答1: unsigned myFunction() {return 0;} template <typename Head, typename... Tail> unsigned myFunction(const Head & head, const Tail &... tail) { return sizeof head + myFunction(tail

variadic templates with template function names and passing arguments and return values around

家住魔仙堡 提交于 2019-12-06 01:40:04
following from this question, I have been trying to create a template function that calls all same-named methods of its mixins. This has been done and verified in the previous question. Now I am attempting to get the return value of SensorType:: Analytically: #include<iostream> #include <string> struct EdgeSensor { void update(int i) { std::cout << "EdgeSensor::update " << i << std::endl; } void updat2(const int i ) { std::cout << "EdgeSensor::updat2" << i << std::endl; } std::string printStats() { std::cout << "EdgeSensor::printStats" << std::endl; return std::string("EdgeSensor::printStats")

C++ polymorphism with variadic function parameter

天大地大妈咪最大 提交于 2019-12-06 01:26:47
I am sharing with you an issue that I got with a class using variadic function parameters. It is the class Thread shown in the following code. It is a wrapper of std::thread in order to use the function pattern. I wanted to use polymorphism with this function in inheriting the class Thread into a new class, Functor, but gcc returns the errors bellow: #include <thread> #include <iostream> using namespace std; template<class... Args> class Thread { public: virtual void operator()(Args...) = 0; void run(Args... args) { std::thread t(std::forward< Thread<Args...> >(*this), std::forward<Args>(args)

Is it possible to get the first type of a parameter pack in a one-liner?

浪尽此生 提交于 2019-12-06 01:01:20
I have a parameter pack given in a variadic template class and want to extract the first type. Currently I do this, which works fine but is somehow cumbersome. Is it possible to do the same thing simpler? FirstEntityType should be defined to have the type of the first type in EntityTs . Note, I want to keep the signature of the class template. I know that it would be possible to write template<typename FirstEntityType, typename... OtherEntityTypes> , it is however something that I don't want to do. template<typename... EntityTs> class EntityContext { template<typename T, typename ... Ts>

Unpacking variadic template parameters into initializer list

拥有回忆 提交于 2019-12-06 00:03:24
I am currently trying to implement a general initializer to reduce the size of our codebase. At one point however, my code looked like this: template<typename T, typename Arg1, typename Arg2> T* ManageDevice(Arg1 arg1, Arg2 arg2) { auto device = new T{ arg1, arg2 }; // More operations on device return device; } template<typename T, typename Arg1, typename Arg2, typename Arg3> T* ManageDevice(Arg1 arg1, Arg2 arg2, Arg3 arg3) { auto device = new T{ arg1, arg2, arg3 }; // More operations on device return device; } template<typename T, typename Arg1, typename Arg2, typename Arg3, typename Arg4> T*

Variadic template unrolling to std::tuple

a 夏天 提交于 2019-12-05 21:52:18
I have a filter class that takes two template parameters, the number of inputs and the number of outputs. template<int Ins, int Outs> class Filter { // implementation }; Sometimes I need to chain multiple filters in series so I was thinking to wrap them in a class template<int... args> class Chain { }; so that when I use the chain Chain<5, 10, 25, 15> chain; it unrolls the args into a tuple to end up with something like this in the Chain class std::tuple<Filter<5, 10>, Fiter<10, 25>, Filter<25, 15>> filters; Is something like this possible? I'm fairly new to these concepts and can't wrap my

Is it possible to pass va_list to variadic template?

随声附和 提交于 2019-12-05 21:02:00
I know that va_list is usually something you should avoid since its not very safe, but is it possible to pass the arguments from a function like: void foo(...); to a function like template<typename... Args> void bar(Args... arguments); ? edit: Originally I wanted to try to use this to call a virtual function with a variable amount of arguments / types, but this was not the way to go making this question kind of irrelevant. Eventually I ended up doing something like this: struct ArgsPackBase { virtual ~ArgsPackBase() {} }; template<typename... Args> struct ArgsPack : public ArgsPackBase {