variadic-templates

How would I 'generate variadic parameters'?

谁说我不能喝 提交于 2019-12-04 15:59:38
I need a way to pass a variable amount of parameters to a function in this circumstance: template<typename ...T> struct Lunch { Lunch(T...){} }; template<typename T> T CheckLuaValue(lua_State* luaState,int index) { //Do Stuff return value; } template <class MemberType, typename ReturnType, typename... Params> struct MemberFunctionWrapper <ReturnType (MemberType::*) (Params...)> { static int CFunctionWrapper (lua_State* luaState) { ReturnType (MemberType::*)(Params...) functionPointer = GetFunctionPointer(); MemberType* member = GetMemberPointer(); int index = 1; //Get a value for each type in

C++ parsing function-type template argument

允我心安 提交于 2019-12-04 14:53:23
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 <typeinfo> template <typename T> class A {}; template <typename Res, typename... Args> class A<Res (Args...

Can't add perfect forwarding to wrapper function

妖精的绣舞 提交于 2019-12-04 12:23:00
While answering this question I wrote this working code, wrapping function passed in template arguments: template<typename Fn, Fn fn, typename... Args> auto wrapper(Args... args)->decltype(fn(args...)){ return fn(args...); } #define WRAPPER(FUNC) wrapper<decltype(&FUNC), &FUNC> Example usage (I use this code for testing): int min(int a, int b){ return (a<b)?a:b; } #include<iostream> using std::cout; int main(){ cout<<WRAPPER(min)(10, 20)<<'\n'; } Two people told me to use perfect forwarding . When I asked how to do this, one of them redirected me here . I read question, carefully read best

What's va_arg() in C++11 variadic template? [duplicate]

五迷三道 提交于 2019-12-04 12:17:41
问题 This question already has answers here : C++ index of type during variadic template expansion (3 answers) Closed 5 years ago . I've read some articles about this new C++11 feature but I didn't understand all stuff (I'm new to C++). How do I access a specific argument like I can do using va_arg from stdarg.h in C? template <typename ... Args> void f(Args ... args) { for(size_t i = 0; i < sizeof ...(args); i++) { // obviously, args...[i] didn't work... } } 回答1: The problem is with TYPE var =

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

天涯浪子 提交于 2019-12-04 12:16:10
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 arguments. So are we just restricted to provide two arguments (key , value) even if the the function has

Variadic template function accepting lambda

隐身守侯 提交于 2019-12-04 11:59:59
I'm trying to understand the compiler error that I'm getting fo the code below. I've got a variadic template function which accepts a lambda with the specified types, and attempting to call that function results in the template not being considered a valid candidate due to a mismatch. #include <functional> template<typename ... ResultTypes> void executeWithResultHandler(std::function<void (ResultTypes...)> lambda) { } int main(int argc, char **argv) { executeWithResultHandler<int>([] (int arg) { }); return 0; } This results in the following error: $ c++ -std=c++11 reduction.cpp reduction.cpp

Build function parameters with variadic templates

霸气de小男生 提交于 2019-12-04 11:26:04
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, double b, const char *c) { Cout() << "Print3:" << a << ", " << b << ", " << c << "\n"; return 1; } main

Ambiguous call to variadic template function with no parameters?

风流意气都作罢 提交于 2019-12-04 11:25:08
When running this: template <typename T> struct CodeByType { static const int32_t Value = 7; }; template <> struct CodeByType<int> { static const int32_t Value = 1; }; template <typename Arg, typename... Args> int32_t Sum() { // The compiler complains on this line return Sum<Arg>() + Sum<Args...>(); } template <typename Arg> int32_t Sum() { return CodeByType<Arg>::Value; } int main() { auto sum = Sum<int, char, double>(); } I'm getting: Error C2668 'Sum': ambiguous call to overloaded function Can someone please explain why and how to overcome it? This looks awfully similar to the below code,

How can I curry variadic template template parameters?

≯℡__Kan透↙ 提交于 2019-12-04 10:41:45
问题 Variadic template template parameters accept any template: template<typename T> struct Test1 { using type = int; }; template<typename T, typename T1> struct Test2 { using type = char*; }; template<template<typename...S> class BeCurry> struct Currying { }; using curry = Currying<Test1>; using curry2 = Currying<Test2>; I want Currying template template class. It means: if the parameter accepts one template param as Test1 , curry::apply<T>::type get Test1<T>::type . If the paramter accepts two

What exactly is a “trailing parameter pack”

我的梦境 提交于 2019-12-04 10:17:43
问题 In resolving ambiguities between function template overloads, partial ordering is performed (see here for some explanations). In that website, we also learn that In case of a tie, if one function template has a trailing parameter pack and the other does not, the one with the omitted parameter is considered to be more specialized than the one with the empty parameter pack. Now, I wonder what precisely a trailing parameter pack is. Which if any of template<class ...> struct tuple { /* ... */ };