variadic-templates

Variadic construction for initialising vector of unique_ptr to base type

点点圈 提交于 2019-12-08 05:50:43
问题 Below is an example program where a 'Container' class needs to store a list of 'Items' via a base class pointer. Having started with C++11/14 the natural choice would be to use std::unique_ptr and variadic templates in the case I have. However being new I cannot fathom how to convert the variadic list into and initialiser list for the vector of unique_ptr in a manner that compiles. Help is greatly appreciated as I have failed to find anything online that helps me with this issue so far

type deduction from char array to std::string

你离开我真会死。 提交于 2019-12-08 04:11:15
问题 I'm trying to write sum function using variadic template. In the code I would write something like sum(1, 2., 3) and it will return most general type of the sum (in this case - double ). The problem is with characters. When I call it like sum("Hello", " ", "World!") template parameters are deduces as const char [7] for example, thus it won't compile. I found the way to specify last argument as std::string("World!") , but it's not pretty, is there any way to achieve automatic type deduction to

How to set the same type for all arguments in this example?

孤者浪人 提交于 2019-12-08 03:30:49
问题 Just for practice I'm trying to write a variadic template that inputs some values into vector. I wrote the following: template <class T> void add(vector<T> *v, T n){ v->push_back(n); } template <class T, class... T2> void add(vector<T> *v, T n, T2... rest){ v->push_back(n); add(v, rest...); } To test these I use the following: vector<int> vI; add(&vI, 10, 30, 25); for (int i = 0; i < vI.size(); i++) cout << vI[i] << endl; Everything works as expected, but I'm wondering if the second template

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

半腔热情 提交于 2019-12-08 02:32:12
问题 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

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

落花浮王杯 提交于 2019-12-08 01:34:04
问题 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>

Store a function with arbitrary arguments and placeholders in a class and call it later

强颜欢笑 提交于 2019-12-08 00:08:31
问题 So I am creating a type of event handler and I am in the process of writing an "Event Listener Wrapper", if you will. The basic idea is this: When you want to subscribe to an event, you create a function that should be called when the event fires. <-- already have that done (kinda, I'll explain) You put this listener function into a wrapper to pass the function onto the dispatcher. The dispatcher gets an event, finds the wrapper for you listener, and calls the underlying function with the

Why does this variadic template specialization definition not compile?

亡梦爱人 提交于 2019-12-07 20:04:52
问题 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..

SFINAE in variadic constructor

谁说胖子不能爱 提交于 2019-12-07 19:51:16
问题 I want to define a generic strong alias type, i.e. a type template<typename T, auto ID = 0> class StrongAlias { T value; }; such that for a type T a StrongAlias<T> can be used in exactly the same way as T , but StrongAlias<T, 0> and StrongAlias<T, 1> are different types that can not be implecitly converted to each other. In order to mimic a T as perfectly as possible, I would like my StrongAlias to have the same constructors as T . This means I would like to do something like the following:

emplacing object with variadic constructor into map

旧城冷巷雨未停 提交于 2019-12-07 19:18:42
问题 I have a movable (but not copyable) type with a variadic template constructor struct foo { std::string a,b,c; foo(foo const&)=delete; foo(foo&&)=default; template<typename...T> for(T&&...); }; and I want to add it into a map: template<typename...T> void add_foo(std::map<std::string,foo>&map, const char*name, T&&...args) { map.emplace(std::piecewise_construct, // how? perhaps like this? std::forward_as_tuple(name), // no: std::forward_as_tuple(args)); // error here } 回答1: std::forward_as_tuple

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

三世轮回 提交于 2019-12-07 16:02:02
问题 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