variadic-templates

Build function parameters with variadic templates

非 Y 不嫁゛ 提交于 2020-02-27 08:23:25
问题 I have this sample code, which do what I need for a 3-parameter function : template<typename T>T GETPARAM(void) { return T(); } template<>int GETPARAM(void) { return 123; } template<>double GETPARAM(void) { return 1.2345; } template<>const char *GETPARAM(void) { return "hello"; } template<typename P1, typename P2, typename P3, typename RES> RES BuildArgs3(RES(*fn)(P1, P2, P3)) { P1 p1 = GETPARAM<P1>(); P2 p2 = GETPARAM<P2>(); P3 p3 = GETPARAM<P3>(); return fn(p1, p2, p3); } int print3(int a,

Pass N-D array by reference to variadic function

a 夏天 提交于 2020-02-25 06:43:25
问题 I'd like to make the function multi_dimensional accept a multidimensional array by reference. Can this be done with a variation of the syntax below which works for three_dimensional ? #include <utility> // this works, but number of dimensions must be known (not variadic) template <size_t x, size_t y, size_t z> void three_dimensional(int (&nd_array)[x][y][z]) {} // error: parameter packs not expanded with ‘...’ template <size_t... dims> void multi_dimensional(int (&nd_array)[dims]...) {} int

Pass N-D array by reference to variadic function

左心房为你撑大大i 提交于 2020-02-25 06:41:06
问题 I'd like to make the function multi_dimensional accept a multidimensional array by reference. Can this be done with a variation of the syntax below which works for three_dimensional ? #include <utility> // this works, but number of dimensions must be known (not variadic) template <size_t x, size_t y, size_t z> void three_dimensional(int (&nd_array)[x][y][z]) {} // error: parameter packs not expanded with ‘...’ template <size_t... dims> void multi_dimensional(int (&nd_array)[dims]...) {} int

Variadic base class using declaration fails to compile in MSVC

夙愿已清 提交于 2020-02-04 20:57:19
问题 I'm trying to implement a variadic visitor class. template<typename T> class VisitorBaseFor { protected: virtual ~VisitorBaseFor() = default; public: virtual void visit(T &t) = 0; }; template<typename... Ts> class VisitorBase : public VisitorBaseFor<Ts>... { public: using VisitorBaseFor<Ts>::visit...; }; I know from that overload trick that variadic using declarations should be possible, but MSVC does not compile my code saying I need to expand Ts while both GCC and Clang compile my code

Is it legal to use variadic templates in operator overloading?

强颜欢笑 提交于 2020-01-30 04:17:30
问题 I would like to be able to write something along these lines: struct bar {}; template <typename ... Args> bar operator+(bar, Args ...) {} I just checked with clang/gcc and the overloaded operator is picked up both by binary expressions ( a+b ) and unary expressions ( +a ), as I would expect. However operators are more restricted than normal functions, in the sense that - for instance - you cannot overload operator+() with three arguments. Is the usage above legal and portable? EDIT To give a

variadic templates parameter matching in std::function

爱⌒轻易说出口 提交于 2020-01-28 06:40:06
问题 I have the following code: #include <iostream> #include <functional> template<typename Return, typename... Params> void func(std::function<Return(Params... )> x) {} void f(double) {} int main() { //func<void, double>(f); // compile error here in the variadic case func(std::function<void(double)>(f)); } I have 2 questions: 1. I do not understand why does the line func<void, double>(f); give me a compiling error /Users/vlad/minimal.cpp:10:5: error: no matching function for call to 'func' func

variadic templates parameter matching in std::function

别等时光非礼了梦想. 提交于 2020-01-28 06:39:34
问题 I have the following code: #include <iostream> #include <functional> template<typename Return, typename... Params> void func(std::function<Return(Params... )> x) {} void f(double) {} int main() { //func<void, double>(f); // compile error here in the variadic case func(std::function<void(double)>(f)); } I have 2 questions: 1. I do not understand why does the line func<void, double>(f); give me a compiling error /Users/vlad/minimal.cpp:10:5: error: no matching function for call to 'func' func

variadic templates parameter matching in std::function

落花浮王杯 提交于 2020-01-28 06:39:30
问题 I have the following code: #include <iostream> #include <functional> template<typename Return, typename... Params> void func(std::function<Return(Params... )> x) {} void f(double) {} int main() { //func<void, double>(f); // compile error here in the variadic case func(std::function<void(double)>(f)); } I have 2 questions: 1. I do not understand why does the line func<void, double>(f); give me a compiling error /Users/vlad/minimal.cpp:10:5: error: no matching function for call to 'func' func

How to use std::enable_if with variadic template

最后都变了- 提交于 2020-01-25 03:50:15
问题 I am trying to create a Tensor class (for those who don't know this is the mathematical equivalent of a multi-dimensional array), and I wish to only allow it to compile if instantiated with a type which satisfies certain type-traits. Ordinarily, I would do something like: template <typename T, std::size_t Size, typename Enable = void> class Foo; // Only allow instantiation of trivial types: template <typename T, std::size_t Size> class Foo<T, Size, typename std::enable_if<std::is_trivial<T>:

Is there a way to convert a list of lvalues and rvalues to a tuple with reference types and full types respectively?

北城以北 提交于 2020-01-24 12:18:54
问题 So given the following 2 functions: int rvalue(); int& lvalue(); The following would be valid: std::tuple<int&, int> x = appropriate_fn(lvalue(), rvalue()); I was thinking something like it like this: template <typename T, typename...Ts> auto make_comparible(T const& arg, Ts&&...args) { return std::make_tuple(T(arg), make_comparible(args...)); } template <typename T, typename...Ts> auto make_comparible(T& arg, Ts&&...args) { return std::make_tuple<T&>(arg, make_comparible(args...)); }