Questions tagged [variadic-templates]

1

votes
2

answer
61

Views

Selecting Variadic Type with Runtime Index using Lookup Table

Consider a variadic pack of types. It's possible to select and use one of those types indexed by a runtime value using a recursive indexing function and a lambda with an auto parameter as follows: #include #include #include template struct identity { using type = T; }; template struct variadic {...
rtek
1

votes
1

answer
30

Views

Store unexpanded parameter pack

Basically I have a variadic template function like so: template void foo(std::string message, Args... args) { //Some nice code } I now wanted to have a struct, which stores the values, which I use later to call this function. I tried it like this: template struct Foo { std::string message; Args args...
Josef Zoller
1

votes
2

answer
46

Views

Performing initialization of templated class using other templated classes in variadic arguments of constructor

I wanted to create a simple HTML dom builder in C++ and decided I would use a templated tag class to describe the type of tag this was. I already used other methods to create the DOM in C++ with some success, but the design wouldn't handle raw strings, so the move to a templated class may assist me...
J. Willus
6

votes
2

answer
209

Views

Why does the following program not select the argument of the same type as the first template parameter?

I am trying to write a function such that f(args..) returns the first parameter of type T. The following program seems to always select the first specialization thus printing 97 (ASCII code of 'a'). Though the second one wouldn't require converting char to int. Could someone please explain the beha...
Abhishek Kumar
1

votes
3

answer
88

Views

How to Write a Lambda Wrapping a Function with Optional Return Value

I have tried to write a lambda that measures the execution time of arbitrary functions. With a lot of help I have managed that for C++14 and functions having a return value, see Measure execution time of arbitrary functions with C++14 lambda. Then I wanted my code to also work with C++11, therefore...
Benjamin Bihler
1

votes
1

answer
76

Views

Can I have const parameter packs?

Alright so Working on a simple logging system for the fun of it, came across an interesting issue. I thought it would be nice to be able to write something along the lines of: Log(Info, 'Result: {}', value); To give me a result like Result: 45 (Using fmt's formatting style here.) So I set up the fun...
Nemo
1

votes
1

answer
74

Views

Failure at disambiguating overloaded method

What follows is reduced from a system that stores pointers to methods along with their parameter's types. The user just provides type::method and the template machinery does the rest. When the method is overloaded the user must provide the signature of the desired method. This was working very well...
user643005
1

votes
1

answer
117

Views

Variadic template taking reference to data types

I would like to create a wrapper for class methods so I used a variable template and std::tuple to wrap the method. It works as I need, but I cannot figure out how to be able to wrap a method with parameters called by reference. Please see the main function below. #include #include #include using...
vitakot
1

votes
0

answer
82

Views

Converting variadic template to variadic function

Given the following case: #include enum class Event { None, Resize, /*Some Other Events*/ }; template struct validator; template using eventor = typename validator::type; template struct validator { using type = void(*)(unsigned int, unsigned int); }; struct My_Event { unsigned int size; void** fun...
Juan
1

votes
1

answer
69

Views

Parameter pack and member function pointer type deduction

I have a little issue with clang and parameter packages. The clang version is: Apple LLVM version 9.0.0 (clang-900.0.38) Target: x86_64-apple-darwin16.7.0 Thread model: posix I have a problem I managed to reproduce in the following snippet: class Foo{ public: int fIsOk; bool fIsNotOk; bool fTheThird...
jetstream
1

votes
1

answer
58

Views

Tuple with Variadic Templates compile error

I am trying to learn variadic templates with a self made tuple implementation. Could someone please explain to me why the following results in a compile error ? namespace my { // Template definition template struct tuple; template struct tuple : public tuple { tuple(T t, Ts... ts) : tuple(ts...)...
lightsunray
1

votes
0

answer
74

Views

Is a is_variadic type trait possible in C++17?

Is it possible, in C++17, to design a type trait which would detect whether a callable is variadic (and therefore can take an arbitrarily long number of parameters) or not? template struct is_variadic; I currently do not see how to do that, but I cannot convince myself that it's not doable. So if i...
Vincent
1

votes
0

answer
66

Views

Compilation error while passing two variadic parameter in c++11

I need to queue task as function pointers. The argument for function pointer is a list of parameters and a callback. I created a class which stores theses tasks. Now I need to write a template method which takes a function pointer, parameters and callback. I tried creating below class, but is not co...
Ranjith
1

votes
0

answer
97

Views

C++ store Class template objects with different specialization inside unique std collection

I have an Action class specialized through templates. I would like to store different specialization of Action types inside an unique std c++ collection (std::map, std::vector, etc...). Here the Action classes: #include #include #include template class Action { private: std::function function; pu...
Squerut
1

votes
0

answer
23

Views

varadic argument templated object creation on container not working

Below is a example (not the entire class, just relevant parts) class Container { private: size_t m_next_id; std::unordered_map m_objects; public: template size_t create(Args... args) { return this->add(new Type (args...)); } size_t add(Base * obj) { m_objects.emplace(m_next_id, obj); return m_next_i...
Glen Fletcher
1

votes
1

answer
78

Views

Fold Expression: parser stackoverflow

Hi I want to use fold expressions , operator but MSVC keeps annoying me with C1026->Program is to complex. I have broken down the problem to minimal example: #include #include template void foo() { //do some stuff } template struct ApplySomeFun; template struct ApplySomeFun { static void execute()...
Caradhras
1

votes
0

answer
23

Views

variadic template with mixed reference and non-reference type

I need a Register some functions with variadic templates like std::function; so i can register class member functions like this: std::function F1 = [this](std::string msg)->void {Fun1(msg); }; RegisterCmd(1, F1); or std::function F = [this](int p1 ,double p2)->void {Fun2(p1,p2); }; RegisterCmd(2,F2...
YNX
1

votes
0

answer
31

Views

Parameter pack and constexpr static

Using parameter pack expansion with rvalue references and const class member data causes a link error on Clang 5 and 6, and GCC 4.8.5 and 5.3. I first encountered the problem with a function with the same signature as std::make_shared but have simplified it to the following test case showing 6 diffe...
Seth Johnson
1

votes
2

answer
48

Views

template Parameter pack expansion on function call with array of data

Trying to avoid overloading my function for x amount of parameters. //T.. Types,C Class,R Return type template R callClassFunction(C& classname,void (C::*func)(T1,T2), void** myd) { return (classname.*func)(*((T1*)myd[0]), *((T2*)myd[1])); } Would it be possible to do this with a variadic template?...
Steven Venham
1

votes
1

answer
29

Views

Deduction error of template parameters of a template template argument

I'm having issues when writing template members of a non template class, specfically when deducing template parameters of a template template argument. The following code, minimal, illustrates my problem. I suspect there are at least two different problems down there. #include #include using names...
R. Absil
1

votes
1

answer
70

Views

Variadic templates have duplicate symbols in object files?

I have the following test program: #include template struct Dispatcher { template static inline void call1(bool a, bool b, int* output, F...) { *output = i; if (a) *output += j; if (b) *output += k; } template static inline void call2(bool a, bool b, int* output, F) { *output = i; if (a) *output +=...
A_K
1

votes
0

answer
80

Views

Converting variadic template arguments to static constexpr array

namespace ct_parser { namespace variadic { template struct static_array { static const char value[sizeof...(Cs)]; static constexpr char val[sizeof...(Cs)] = { Cs... }; }; template const char static_array::value[sizeof...(Cs)] = { Cs... }; Later... const char * buf3 = ct_parser::variadic::static_a...
Lourens Dijkstra
1

votes
1

answer
41

Views

How can I fix my compilation errors with this template which uses a variadic template variable

I am learning to code with variadic template and wrote a simple variadic template to experiment. The compiler complains about the m_bindingObj member variable with template argument 1 is invalid. What I am missing? #include #include class ClassA { public: static ClassA * construct(int varA, int va...
Guy B
1

votes
3

answer
44

Views

Specializing method template while checking for subtype

What is the syntax to specialize a template for a method whilst also checking that some subtype exists? // Syntax works fine for functions template void my_function(); template void my_function() { } // Doesn't work for methods class MyClass { template void my_method(); }; // Commenting out the nex...
charmoniumQ
16

votes
1

answer
426

Views

Variadic deduction guide not taken by g++, taken by clang++ - who is correct?

Consider the following code: template struct list { template list(Args...) { static_assert(sizeof...(Types) > 0); } }; template list(Args...) -> list; int main() { list l{0, 0.1, 'a'}; } I would expect decltype(l) to be list. Unfortunately, g++ 7.2 and g++ trunk fail the static assertion. clang+...
Vittorio Romeo
1

votes
0

answer
43

Views

clang warning: -Wambiguous-ellipsis

When implementing is_function, I trying to enum every case manually with macro: #define ImplIsFun(QUALIFIERS) \ template \ struct is_function : public true_ \ {}; \ template \ struct is_function : public true_ \ {}; // clang-format off ImplIsFun() ImplIsFun(const) ImplIsFun(&) ImplIsFun(&&) ImplIsFu...
陳 力
1

votes
2

answer
60

Views

How to transform this list of types with brigand?

I have the following list of types: using ComponentList = brigand::list How can I transform the previous list into a new list that would look like this: using ComponentHandleList = brigand::list; I basically want to generate a new list that has wrappers for every element in the previous list. I tri...
Bryan Triana
1

votes
2

answer
71

Views

How does this variadic template with using work?

I have this code snippet and am trying to understand how it works: #include class GenBase { public: void BaseClass() { std::cout
Eugene
1

votes
0

answer
44

Views

Parameter Pack Iterating Backwards when evaluating function in argument list

I am learning parameter packing in C++ and am confused by the output of this snippet: template void f1(Ts... args) { std::cout
superjax
1

votes
0

answer
45

Views

How to compile c++ code that uses recursive variadic templates with icpc17?

I want to compile a variadic template based version of a matrix vector product with the icpc 17 compiler (icpc (ICC) 17.0.4 20170411). The code compiles without any issues both with clang and gcc. However, with icpc the compilation seems to hang (I waited for about an hour). Does anybody know of a d...
sudoer
1

votes
2

answer
63

Views

constructor with variadic multidimentional initializer_list

I have the following constructor: class A { template A(std::initializer_list && ... args); } And I have the code for that class: A a1 = {{5.0f, 6.0f}, {-7.0f, 8.0f}}; A a2 = {{{5.0f, 3.f}, {6.0f, 7.f}}, {{-7.0f, 9.f}, {12.f, 8.0f}}}; a1 can be compiled and a2 can't be compiled. I understand, curly b...
ffilosoff
1

votes
1

answer
69

Views

Variadic template parameter order problem

I have a templated function wrapper that I am attempting to update to C++11 syntax (variadic paremeters). My issue is that I am caught in a 'catch 22' where 'Args...' must be the last template parameter, but at the same time, cannot be defined after the function pointer template parameter. Any idea...
JWellbelove
1

votes
1

answer
124

Views

C++: confusing variadic template causing “candidate function not viable” error

I was trying to port a CUDA project into ROCm platform which make use of C++ templates extensively. During this process, I am getting following compilation error /root/warp-ctc/include/detail/gpu_ctc.h:381:5: error: no matching function for call to 'hipLaunchKernelGGL' hipLaunchKernelGGL((prepare_s...
harish2704
1

votes
0

answer
72

Views

Get tuple of inherited base classes' types

I was looking for a way to get a tuple pack of all the inherited classes, couldn't find one. Intend: I need to statically assert if a class inherits from a template base class. Template parameter is to be deduced from a specialization of a base template, given that I can deduce it's typename in the...
Sergey Kolesnik
1

votes
1

answer
604

Views

(Simple) constructors in variadic template classes

How would a constructor and a copy constructor look for this variadic template class? struct A {}; struct B {}; template < typename Head, typename... Tail> struct X : public Head, public Tail... { X(int _i) : i(_i) { } // add copy constructor int i; }; template < typename Head > struct X { }; int ma...
Allan
1

votes
1

answer
784

Views

“No matching function for call” with variadic templates

I have a class that is designed to dynamically load a .dll or .so or equivalent. From there, it will return pointers to whatever function you're trying to find. Unfortunately, I've come across two issues in my implementation. If I use the 'dumb' function returning void* as pointers to functions, I g...
mechanarchy
1

votes
1

answer
340

Views

Variadic template candidate unmatched

I'm attempting to put together a generic method invoker (for a C++ OO/v8 bridge), using variadic template metaprogramming to build the parameter list, converting to native types, and finally execute the attached method, once the incoming param list is empty (and outgoing is therefore built): templat...
jka6510
1

votes
2

answer
241

Views

C++ Trying to create an 'Intermediate' functor

What I mean by an 'intermediate' functor is: a normal functor where one of the arguments can be specified at call-time. The problem is that I have an animation timeline (essentially scalar values at particular frames), and the output of this needs to be piped to and from getter/setter methods withi...
cmannett85
1

votes
2

answer
185

Views

check types passed to variadic template

I would like to achieve something like that: template class MyClass{ public: MyClass(){ for(auto arg : {sizeof(Args)...}) std::cout
Dejwi
1

votes
2

answer
111

Views

How do I convert a basic code snippit/pattern to use c+11 variadic templates

The following simple code pattern is very common in graphics programming. It creates an array of layers and loops over them. struct Layer { int n; void operator()(float value) { } }; struct AnotherLayer { int n; int m; void operator()(float value) { } }; void process_layers(Layer_t* layer, size_t...
Tom Brinkman

View additional questions