variadic-templates

How to make variadic template class method take function pointer as argument with type derived from function template?

允我心安 提交于 2019-12-22 08:55:32
问题 Sorry the title is a mouthful. I'm working on an array class similar to the one discussed here. I want to define a "map" function that takes a user-defined function and applies it to each element of the array. For the purposes of type-checking, I'd like to define it such that the user-specified function must take the same number of arguments as are passed to the map function, so that double f(double a, double b) { return a + b; } Array<double,2> x, y, z; x.map(f, y, z); will compile but

How to fill array with contents of a template parameter pack?

不羁的心 提交于 2019-12-22 08:09:24
问题 I had nested partially specialized template code working with VS 2015 until I discovered that it was not standards-compliant. I want it to be so I twisted my code to overcome the former issue and also that one and have now hit a hard wall. Using variadic templates and partial specialization I would like to fill an array at compile-time given a fixed set of parameters. What I want to achieve also seems similar to this answer but I did not manage to make it work. Consider the following program:

C++ Vector wrapper around variadic function

笑着哭i 提交于 2019-12-22 08:06:13
问题 I have an api that looks like this: template<typename... Args> Widget::Widget(std::string format_str, Args&&... args); How would you call this method if you have a vector of strings for 'args', i.e. the args length isn't known at compile time? What would the implementation of a wrapper function look like that would convert this to something like this? template<typename... Args> Widget::WrapperWidget(std::string format_str, vector<string>); 回答1: Following may help: #if 1 // Not in C++11

How to swap two parameters of a variadic template at compile time?

房东的猫 提交于 2019-12-22 05:23:15
问题 I'm trying to swap two parameters of a variadic template at compile time : template<int...Numbers>struct sequence{}; template<size_t first,size_t second> struct Swap_Pair { const static size_t First = first; const static size_t Second = second; }; template <int...Numbers,class swap_pair> struct Swap_Data { static std::array<int, sizeof...(Numbers)> data_;//How to swap Numbers base on the pair and store it in data_ ? }; The use case should be : sequence<1, 2, 3, 4, 5, 6> array; auto result =

Why does std::visit take a variable number of variants?

依然范特西╮ 提交于 2019-12-22 01:26:44
问题 Trying to get more familiar with C++17, I've just noticed std::visit: template <class Visitor, class... Variants> constexpr /*something*/ visit(Visitor&& vis, Variants&&... vars); Why does std::visit not take a single variant, but rather any number of variants? I mean, you can always take some standard library function and have it take multiple parameters with the same role, working on all of them (e.g. std::find() for multiple elements in a container); or you could be taking multiple

Passing any function as a template parameter?

蹲街弑〆低调 提交于 2019-12-22 00:34:34
问题 I am looking for a way to pass a generic (constexpr, obviously) function to a template. It has to be able to take any amount of parameters, without using a lambda. This is what I have so far: template<typename T, T(*FUNC)()> struct CALL { static inline constexpr decltype(FUNC()) EXEC() { return FUNC(); } }; This however only works if the passed function takes no parameters. Is there a way to make the template accept ANY constexpr function? Passing a std::function does not seem to work. I

Calculate the average of several values using a variadic-template function

北城余情 提交于 2019-12-21 21:21:06
问题 I am trying to write a function to determine the average of an arbitrary number of arguments, all of which have the same type. For learning purposes I am trying to do this using a variadic-templated function. This is what I have so far: template<typename T, class ... Args> T Mean(Args ... args) { int numArgs = sizeof...(args); if (numArgs == 0) return T(); // If there are no arguments, just return the default value of that type T total; for (auto value : args...) { total += value; } return

C++ parsing function-type template argument

吃可爱长大的小学妹 提交于 2019-12-21 19:59:22
问题 I want to be able to call a function with a template parameter that is of function type (including parameter and return types), i.e. double(int, long) , and in the function separate the types and use them individually. For example I want to be able to call a function printRes<double(int, long)>(); This function above should parse the template argument and extract the return type double and output it. I know how to do this using a class and variadic templates: #include <iostream> #include

Folding over arbitrarily many variadic packs

依然范特西╮ 提交于 2019-12-21 18:29:15
问题 I'm reading through Eric Niebler's post on his tiny metaprogramming library. In trying to implement the pieces that he omits / lists as challenges, I am left with the following implementation of transform : template <typename F, typename... As> using meta_apply = typename F::template apply<As...>; template <typename... > struct typelist_transform; // unary template <typename... T, typename F> struct typelist_transform<typelist<T...>, F> { using type = typelist<meta_apply<F,T>...>; }; //

use of emplace(args&& …) in associative containers

风格不统一 提交于 2019-12-21 18:02:15
问题 I am trying to forward some arguments to do inplace construction of objects . I don't quite get the rationale behind the usage of emplace in associative containers or may be I am just using/thinking in a wrong way. It would be great if someone can share code snippets for usage. Associative container like map always store an object of kind pair() , and the emplace function says that it will call the constructor of the object stored (which for is always pair in case of maps) by forwarding the