variadic-templates

How do I get the argument types of a function pointer in a variadic template class?

萝らか妹 提交于 2019-12-17 05:51:09
问题 This is a follow up of this problem: Generic functor for functions with any argument list I have this functor class (full code see link above): template<typename... ARGS> class Foo { std::function<void(ARGS...)> m_f; public: Foo( std::function<void(ARGS...)> f ) : m_f(f) {} void operator()(ARGS... args) const { m_f(args...); } }; In operator() I can access the args... easily with a recursive "peeling" function as described here http://www2.research.att.com/~bs/C++0xFAQ.html#variadic-templates

How do I get the argument types of a function pointer in a variadic template class?

别来无恙 提交于 2019-12-17 05:51:02
问题 This is a follow up of this problem: Generic functor for functions with any argument list I have this functor class (full code see link above): template<typename... ARGS> class Foo { std::function<void(ARGS...)> m_f; public: Foo( std::function<void(ARGS...)> f ) : m_f(f) {} void operator()(ARGS... args) const { m_f(args...); } }; In operator() I can access the args... easily with a recursive "peeling" function as described here http://www2.research.att.com/~bs/C++0xFAQ.html#variadic-templates

recursive variadic template to print out the contents of a parameter pack

萝らか妹 提交于 2019-12-17 04:54:47
问题 How is it possible to create a recursive variadic template to print out the contents of a paramater pack? I am trying with this, but it fails to compile: template <typename First, typename ...Args> std::string type_name () { return std::string(typeid(First).name()) + " " + type_name<Args...>(); } std::string type_name () { return ""; } How shall I end the recursion? 回答1: You need to use partial specialisation to end the recursion, but since you can't partially specialise free functions in C++

How to store variadic template arguments?

元气小坏坏 提交于 2019-12-17 04:41:18
问题 Is it possible to store a parameter pack somehow for a later use? template <typename... T> class Action { private: std::function<void(T...)> f; T... args; // <--- something like this public: Action(std::function<void(T...)> f, T... args) : f(f), args(args) {} void act(){ f(args); // <--- such that this will be possible } } Then later on: void main(){ Action<int,int> add([](int x, int y){std::cout << (x+y);}, 3, 4); //... add.act(); } 回答1: To accomplish what you want done here, you'll have to

How to store variadic template arguments?

馋奶兔 提交于 2019-12-17 04:41:14
问题 Is it possible to store a parameter pack somehow for a later use? template <typename... T> class Action { private: std::function<void(T...)> f; T... args; // <--- something like this public: Action(std::function<void(T...)> f, T... args) : f(f), args(args) {} void act(){ f(args); // <--- such that this will be possible } } Then later on: void main(){ Action<int,int> add([](int x, int y){std::cout << (x+y);}, 3, 4); //... add.act(); } 回答1: To accomplish what you want done here, you'll have to

How to store variadic template arguments?

核能气质少年 提交于 2019-12-17 04:41:05
问题 Is it possible to store a parameter pack somehow for a later use? template <typename... T> class Action { private: std::function<void(T...)> f; T... args; // <--- something like this public: Action(std::function<void(T...)> f, T... args) : f(f), args(args) {} void act(){ f(args); // <--- such that this will be possible } } Then later on: void main(){ Action<int,int> add([](int x, int y){std::cout << (x+y);}, 3, 4); //... add.act(); } 回答1: To accomplish what you want done here, you'll have to

Variadic function template with pack expansion not in last parameter

假如想象 提交于 2019-12-17 04:08:16
问题 I am wondering why the following code doesn't compile: struct S { template <typename... T> S(T..., int); }; S c{0, 0}; This code fails to compile with both clang and GCC 4.8. Here is the error with clang: test.cpp:7:3: error: no matching constructor for initialization of 'S' S c{0, 0}; ^~~~~~~ test.cpp:4:5: note: candidate constructor not viable: requires 1 argument, but 2 were provided S(T..., int); ^ It seems to me that this should work, and T should be deduced to be a pack of length 1. If

Variadic function template with pack expansion not in last parameter

做~自己de王妃 提交于 2019-12-17 04:08:11
问题 I am wondering why the following code doesn't compile: struct S { template <typename... T> S(T..., int); }; S c{0, 0}; This code fails to compile with both clang and GCC 4.8. Here is the error with clang: test.cpp:7:3: error: no matching constructor for initialization of 'S' S c{0, 0}; ^~~~~~~ test.cpp:4:5: note: candidate constructor not viable: requires 1 argument, but 2 were provided S(T..., int); ^ It seems to me that this should work, and T should be deduced to be a pack of length 1. If

How to call a function on all variadic template args?

拟墨画扇 提交于 2019-12-17 03:00:22
问题 I would like to do template<typename... ArgTypes> void print(ArgTypes... Args) { print(Args)...; } And have it be equivalent to this quite bulky recursive chain: template<typename T, typename... ArgTypes> void print(const T& t, ArgTypes... Args) { print(t); print(Args...); } followed by explicit single-parameter specializations for every type I'd like to print. The "problem" with the recursive implementation is that a lot of redundant code is generated, because each recursive step results in

make_unique and perfect forwarding

一笑奈何 提交于 2019-12-16 19:58:39
问题 Why is there no std::make_unique function template in the standard C++11 library? I find std::unique_ptr<SomeUserDefinedType> p(new SomeUserDefinedType(1, 2, 3)); a bit verbose. Wouldn't the following be much nicer? auto p = std::make_unique<SomeUserDefinedType>(1, 2, 3); This hides the new nicely and only mentions the type once. Anyway, here is my attempt at an implementation of make_unique : template<typename T, typename... Args> std::unique_ptr<T> make_unique(Args&&... args) { return std: