variadic-templates

How to declare an “implicit conversion” in a variadic template?

本小妞迷上赌 提交于 2019-12-23 05:11:30
问题 My aim is to send a data to several streams. It is possible by using boost::tee. But I want to write a wrapper with variadic template for using several streams. The problem is that I need an implicit convertation from descendant struct to ancestor struct. Or something like that. #include <boost/iostreams/tee.hpp> #include <boost/iostreams/stream.hpp> #include <fstream> #include <iostream> using namespace std; namespace bio = boost::iostreams; using bio::tee_device; using bio::stream; template

Template function that can take lambda or function pointer and deduce arguments for passing to another template

依然范特西╮ 提交于 2019-12-23 05:10:35
问题 I maintain an open-source lockless threading library designed for high speed parallel loop unrolling which is used in a couple commercial video games. It has incredibly low-overhead, around 8 clocks for creating a message and around 500 (per thread, including latency) for the entire dispatch and remote execute overhead. I say this first to explain why I don't simply use use std::function and bind. The library packages a function call and the arguments to the function call in a message (of

How to declare a typedef that references itself?

梦想与她 提交于 2019-12-23 04:34:26
问题 I have a Variant type which I want to use in something like a JSON parser. A type in JSON can include objects and arrays. These objects and arrays can contain members of their own type: typedef variant<int,float,bool,std::string> baseType; typedef std::vector<baseType> arrayType; // problem, can't have arrays of arrays typedef std::unordered_map<std::string,baseType> objType; // problem, can't have objects or arrays within objects. How do I create a "recursive" templated type? Something like:

When specializing a class, how can I take a different number of template parameters?

不羁岁月 提交于 2019-12-23 04:09:22
问题 I just asked this question: Can I get the Owning Object of a Member Function Template Parameter? and Yakk - Adam Nevraumont's answer had the code: template<class T> struct get_memfun_class; template<class R, class T, class...Args> struct get_memfun_class<R(T::*)(Args...)> { using type=T; }; These is clearly an initial declaration and then a specialization of struct get_memfun_class . But I find myself uncertain: Can specializations have a different number of template parameters? For example,

parameter pack expansion not working in lambda

纵然是瞬间 提交于 2019-12-23 02:45:17
问题 I am learning variadic templates by doing some exercises and I am stuck when it comes to parameter pack expansion in lambda So, my idea is to write a timer class whose payload will be callable, But I get a compilation error when I try to expand the parameter pack inside a lambda function.. gcc version 4.8.4 (Ubuntu 4.8.4-2ubuntu1~14.04.3) template<typename F, typename... Args> struct timer { timer(const std::chrono::milliseconds milliseconds, F call, Args&&... args) { m_timer = std::make

boost::range::join many ranges in one custom call

烈酒焚心 提交于 2019-12-23 02:38:59
问题 The Solution section in gnzlbg's SO question boost::range::join for multiple ranges implies it can join many ranges in one client code call to a custom function variadic template that calls boost::join and boost::make_iterator_range . According to that question, answer, and comments, the prior can join 2 ranges and the latter is needed to ensure the non- const overload of the prior is used. Any containers after the 2nd one are supposedly perfect-forwarded via std::forward . But my client code

Is it possible to pass va_list to variadic template?

最后都变了- 提交于 2019-12-22 11:01:23
问题 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 {

Variadic template unrolling to std::tuple

≯℡__Kan透↙ 提交于 2019-12-22 10:52:45
问题 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

Using non-const expression as template parameter

徘徊边缘 提交于 2019-12-22 10:50:54
问题 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

What is wrong with this variadic templates example?

泄露秘密 提交于 2019-12-22 09:11:45
问题 The base class is : #include <memory> namespace cb{ template< typename R, typename ... Args > class CallbackBase { public: typedef std::shared_ptr< CallbackBase< R, Args... > > CallbackPtr; virtual ~CallbackBase() { } virtual R Call( Args ... args) = 0; }; } // namespace cb Derived class is this : namespace cb{ template< typename R, typename ... Args > class FunctionCallback : public CallbackBase< R, Args... > { public: typedef R (*funccb)(Args...); FunctionCallback( funccb cb_ ) :