variadic-templates

Template argument and deduction of std::function parameters

廉价感情. 提交于 2020-01-24 12:07:38
问题 Assume there is a template function foo() which accepts an arbitrary number of arguments. Given the last argument is always an std::function , how do I implement a foo() template shown below in a way that CbArgs would contain this std::function 's parameters? template<typename... InArgs, typename... CbArgs = ???> // ^^^^^^^^^^^^ void foo(InArgs... args) { ... } For example, CbArgs should be {int,int} if invoked like this: std::function<void(int,int)> cb; foo(5, "hello", cb); My first idea was

ambiguous operator[] in variadic template

十年热恋 提交于 2020-01-23 07:41:46
问题 I'm trying to compile this example, where a variadic class template inherits from a variadic amount of bases, each of which implements a different operator[] : #include <iostream> template <typename T> struct Field { typename T::value_type storage; typename T::value_type &operator[](const T &c) { return storage; } }; template<typename... Fields> struct ctmap : public Field<Fields>... { }; int main() { struct age { typedef int value_type; }; struct last_name { typedef std::string value_type; }

Is it legal to partially specialise variadic template inner class with args from variadic template of an outer class

僤鯓⒐⒋嵵緔 提交于 2020-01-23 04:34:07
问题 Consider the code: #include <iostream> template <class... Ts> struct outer { template <class... ITs> struct inner { static constexpr bool value = false; }; template <class... ITs> struct inner<Ts..., ITs...> { static constexpr bool value = true; }; }; int main() { std::cout << outer<int, float, double>::inner<int, float, double, int>::value << std::endl; } The code compiles with clang++ but not with g++ where it produces an error: temp3.cc:11:11: error: parameter pack argument ‘Ts ...’ must

“Unpack” an array to call a function with variadic template

时间秒杀一切 提交于 2020-01-21 13:21:06
问题 I'm using the ben strasser C++ fast csv parser: https://github.com/ben-strasser/fast-cpp-csv-parser. It uses a variadic template to pass column values back to the while loop that processes the csv data: io::CSVReader<2> in(csv_filename); double x, y; while(in.read_row(x,y)) { //code with x and y } This calls the following function in the CSVReader class: template<class ...ColType> bool read_row(ColType& ...cols){ //snip } This works fine for me with my x and y values. However, I would like to

“Unpack” an array to call a function with variadic template

自闭症网瘾萝莉.ら 提交于 2020-01-21 13:19:34
问题 I'm using the ben strasser C++ fast csv parser: https://github.com/ben-strasser/fast-cpp-csv-parser. It uses a variadic template to pass column values back to the while loop that processes the csv data: io::CSVReader<2> in(csv_filename); double x, y; while(in.read_row(x,y)) { //code with x and y } This calls the following function in the CSVReader class: template<class ...ColType> bool read_row(ColType& ...cols){ //snip } This works fine for me with my x and y values. However, I would like to

Unpacking arguments of a functional parameter to a C++ template class

ⅰ亾dé卋堺 提交于 2020-01-20 04:05:33
问题 I have a question involving functional template arguments to template classes in C++. I'd like to define a template class Foo taking a single template parameter Fun template <typename Fun> struct Foo { ... }; such that given a function like void bar(std::string a, float b, char c) { ... } then Foo<bar>::args_t will be equivalent to a typedef for std::tuple<std::string, float, char> Is this possible? (The use of std::tuple here is just for concreteness. More generally I'm wondering if it's

How to implement a variadic tuple_map operation?

此生再无相见时 提交于 2020-01-17 13:09:24
问题 I want to implement a function which maps a variadic list of tuples to another tuple, given a function. It applies an N-ary function f to a list of elements taken from a list of N tuples (each with size at least M) and creates a new M-element tuple from the result of these applications. For a list of N tuples, each with M elements, the call to std::make_tuple would look like this pseudocode: std::make_tuple( f(t1_1, t2_1, t3_1, ..., tN_1), f(t1_2, t2_2, t3_2, ..., tN_2), f(t1_3, t2_3, t3_3, .

Custom Container emplace with variadic templates

风流意气都作罢 提交于 2020-01-17 04:11:27
问题 I am implementing a simple circular vector class. I want to implement an emplace member function, but I am getting an error which I don't understand. It might be a simple fix for something I am doing wrong, but since I don't have much experience with variadic templates, I can't figure out what... The error I am getting is: main.cpp: In function 'int main()': main.cpp:104:50: error: no matching function for call to 'CircularVector<Item>::emplace(int, int, int, int, int, int, std::vector<int>,

Variadic template, function as argument

流过昼夜 提交于 2020-01-16 16:16:06
问题 I would like to use a function as argument in a variadic template, why does the following not work? How do I make it work? template<typename F, typename... Args> F test(F f, const Args&&... args) { return f(std::forward<Args>(args)...); } int simple(int i) { return i; } int main() { std::cout << test(simple, 2); // error, 'std::forward': none of the 2 overloads could convert all the argument types } 回答1: There are a couple of problems with your code. First of all, you should use forwarding

Passing variadic parameters in an already template-variadic function

我与影子孤独终老i 提交于 2020-01-16 06:55:51
问题 The title is bad but I couldn't come up with anything better. Feel free to change it. Here's a template multidimensional array class that I'm currently working on. I'm trying to optimise it as much as I can: #include <array> template <typename T, std::size_t... Dimensions> class multidimensional_array { public: using value_type = T; using size_type = std::size_t; private: template<typename = void> static constexpr size_type multiply(void) { return 1u; } template<std::size_t First, std::size_t