Questions tagged [variadic]

In computer science, an operator or function is variadic if it can take a varying number of arguments; that is, if its arity is not fixed.

Filter by
Sorted by
Tagged with
24 votes
3 answers
1k views

Formatted print without the need to specify type matching specifiers using _Generic

This code allows printing in C with a style similar to C++ ostream. By using _Generic() (see ...
chux - Reinstate Monica's user avatar
19 votes
3 answers
3k views

Min function accepting varying number of arguments in C++17

Come across this problem once again in the book The Modern C++ Challenge. 18. Minimum function with any number of arguments Write a function template that can take any number of arguments and returns ...
Lingxi's user avatar
  • 818
17 votes
3 answers
15k views

Very basic tuple implementation

I've been messing with metaprogramming and variadic templates in C++, and I came up with this very primitive implementation of a tuple: ...
chbaker0's user avatar
  • 803
14 votes
3 answers
10k views

Apply a function to each element of a tuple ("map" a tuple)

C++ doesn't (yet) have a 'map' higher-order function, certainly not one which applies to a compile-time sequence of values like a tuple. We have std::transform, and ...
einpoklum's user avatar
  • 2,015
12 votes
1 answer
4k views

Variadic function with restricted types

I am designing a class which (among other things) acts as a vertex in an undirected graph. ...
aschepler's user avatar
  • 368
12 votes
1 answer
2k views

Macro to build type declaration

These are some macros to help build a traits class (for the parser/printer classes I am building). Traits.h ...
Martin York's user avatar
  • 95.1k
11 votes
2 answers
1k views

Call a lua function - the C++ way

I'm not that good when it comes to templates, especially when it comes to variadic templates. I want to show you some code where I use both - templates and variadic templates - to call a lua function. ...
tkausl's user avatar
  • 614
11 votes
1 answer
5k views

Enum to string with template metaprogramming

This allows enum values to be outputted as strings. But enum_strings is a type, not a map or a vector, so if the enum value is known during compile time, there ...
prestokeys's user avatar
  • 1,371
10 votes
1 answer
666 views

Variadic templated method to extract values from a table

Is this the correct, most sane way to do variadic templates in C++? All that this function does is to see if gate has a key, if it has, it adds it to a vector of values, if not, it default constructs ...
João Pires's user avatar
9 votes
3 answers
4k views

Partial application in C++

Here is my code: ...
Cameron White's user avatar
9 votes
2 answers
9k views

`printf` improvement with C++ variadic templates

I am currently working through learning C++'s variadic templates and I wanted to create an example use of them that is less trivial than the one in my book. I decided to create a modification of ...
john01dav's user avatar
  • 421
9 votes
1 answer
186 views

Rotate a given number of variables in-place

A very simple but useful algorithm: rotate the values of a given number of variables in-place to the left so that the first variable gets the value of the second, the second gets the value of the ...
Morwenn's user avatar
  • 20k
9 votes
2 answers
1k views

Generating all permutations of a template pack

AllPermutedPacks<Pack<Types...>>::type is to be the pack of packs consisting of all permutations of Types.... For ...
prestokeys's user avatar
  • 1,371
9 votes
2 answers
8k views

A small printf-like function using variadic templates

I'm looking to get some feedback on a small printf-like function I wrote that provides simplified behavior similar to ...
countfromzero's user avatar
8 votes
3 answers
452 views

Generators and Distributions 2.0

In my original question: Generically encapsulating random generators and distributions into a class template I was given some excellent suggestions from two very informative users. I will give a basic ...
Francis Cugler's user avatar
8 votes
3 answers
3k views

The perfect function alias

For many years, I have waited for function aliases in C++. They still aren't here. I've been using macros to generate wrapper functions. This is my best attempt at the perfect function alias: ...
Indiana Kernick's user avatar
7 votes
3 answers
7k views

Sample printf implementation

Here is my code for printf() implementation in C. Please share your thoughts for improvements. ...
Subhajit's user avatar
  • 259
7 votes
3 answers
761 views

Coalescing lvalue references to std-optionals

I want to coalesce (lvalue references to) optionals of the same type, in C++17: ...
einpoklum's user avatar
  • 2,015
7 votes
2 answers
4k views

writeln() and format() with variadic templates

I wanted to get better acquainted with variadic templates, so I decide to try to implement a function like D's writeln(), just for fun. ...
glampert's user avatar
  • 17.1k
7 votes
1 answer
544 views

Implementing checksum add without carry in C

I need to write a function in C which performs a checksum using the rule "add without carry". As I understand it, the "add without carry" concept is equivalent to the bitxor operator and given that ...
zephyr's user avatar
  • 173
7 votes
1 answer
188 views

Extending functions to tuples

Intro: Imagine you have defined addition on types A and B with function A add(A,A) and ...
tom's user avatar
  • 201
7 votes
1 answer
147 views

Turn a vsscanf call into a sscanf call

I am reviewing some C code (see extract below) that uses inline asm. I believe there are 4 spec violations, 1 inefficiency and 1 honking big design flaw in this code. While some of the problems I'm ...
David Wohlferd's user avatar
7 votes
1 answer
928 views

Most elegant variadic functor

Question Suppose we have two sorts of classes an input class Input defines a type result_type defines ...
yannick's user avatar
  • 171
6 votes
2 answers
11k views

Bit packing and unpacking

I needed a variadic function to pack and unpack bits into integer types. This is my first attempt: ...
gd1's user avatar
  • 317
6 votes
3 answers
14k views

Use va_list to format a string

I wrote a function that basically works like sprintf, but instead returns the formatted string (std::string). I haven't worked ...
oliveryas01's user avatar
6 votes
1 answer
3k views

Max function with variable number of arguments

I find it irritating that in standard C++ I can't do std::max(a, b) = x when it's possible and that it can't handle more than 2 arguments. For the second concern I ...
Alexandru Ica's user avatar
6 votes
2 answers
469 views

Small Formatting Library

I am learning C++ and currently playing with variadic template functions and formatting string, So here is my small attempt to write a formatting function and provide a simple iostream-like interface ...
bluedragon's user avatar
6 votes
1 answer
2k views

Test whether a type T is among the types of a tuple

On SO, I asked for a way to do it without variadics. I also provided a typical "normal" implementation with variadics: ...
Nikos Athanasiou's user avatar
6 votes
2 answers
715 views

Custom variadic min/max/sum functions

I have decided to write my own min max sum functions. I really don't like the ones provided ...
Ashock ev's user avatar
6 votes
2 answers
1k views

Typesafe scanf-like function with variadic templates

I'm hoping to get some feedback on a function with variadic templates that parses a format string and fills in some parameters whose order and types are based on the characters in the format string. ...
ptomato's user avatar
  • 161
6 votes
1 answer
216 views

Variadic template for concisely defining a tuple whose components are all the same type

C++11 provides a std::tuple template, but it requires the types of all the fields to be listed individually, which is inconvenient if there are many fields and they'...
Wyzard's user avatar
  • 241
5 votes
3 answers
116 views

C function that emulates string concatenation operator '+' in Python or Go

I'm working on a project of mine which required multiple calls to strncat(), and it came to me that it'd have been much easier if C also had something like ...
debdutdeb's user avatar
5 votes
1 answer
3k views

Concatenate two containers, e.g. vector or strings

I'd like to create a function that can concatenate vectors or strings. It must Keep correct order Be optimal This is the solution I came up with: ...
Mathijs's user avatar
  • 53
5 votes
1 answer
179 views

Meta functions for sequences of exponents of 2

I have implemented some meta functions to create sequence for exponents of 2. I would love to know if there is any way to make the code more optimized. Note: ...
starrk's user avatar
  • 369
5 votes
1 answer
275 views

Multi-dimensional Image Data Structure with Variadic Template Functions in C++

This is a follow-up question for Three dimensional data structure in C++. I am trying to implement multi-dimensional image data structure with variadic template functions. For example, ...
JimmyHu's user avatar
  • 4,708
5 votes
2 answers
504 views

C++ wrapper class to mimic a C array's brace initialization

I have an inheritance class that mimics the behavior of C style array's brace initialization by using a class template that has a variadic constructor but the template itself is not a variadic ...
Francis Cugler's user avatar
5 votes
1 answer
2k views

Generic message system using variadic templates

in my spare time I´m creating a hobby game engine as decided to write some of my systems on my own. As the time passes I´ve decided to start publishing it as a independent libraries for use (so ...
Pins's user avatar
  • 153
5 votes
1 answer
1k views

Thread-Safe Variadic Printing Function

Printing to stdout is thread-safe in many systems when using printf or std::cout, but not in ...
user avatar
5 votes
1 answer
595 views

`zip` operator to iterate on multiple container in a sign

I worked out a zip operator similar to Python's, because I didn't find one in std. It allows to use range-based ...
mqtthiqs's user avatar
  • 153
5 votes
3 answers
983 views

Encapsulating snprintf to avoid repetition of sizeof

Correct usage of snprintf involves very long and repetitive lines: ...
alx - recommends codidact's user avatar
5 votes
1 answer
469 views

Using templates to wrap variardic JNI method calls into type-safe C++ functors

I am working on a project which uses JNI. More than once I fell over the absence of type safety working with the variardic function calls. So I came up with this module. It wraps the variardic JNI ...
Hermann's user avatar
  • 161
4 votes
4 answers
3k views

A variadic C function for concatenating multiple strings

I have this function that takes a number \$N\$, and \$N\$ C strings, concatenates and returns the result: ...
coderodde's user avatar
  • 28.2k
4 votes
3 answers
463 views

Generically encapsulating random generators and distributions into a class template

I previously had a set of classes in one of my older libraries for working with the random number generators and distributions that was written when Visual Studio 2008 - 2010 were commonplace and ...
Francis Cugler's user avatar
4 votes
2 answers
80 views

Header-only variadic console output and runtime input validation in C++ using templates

Template IO Module This is a single-file, header-only input validation with two variadic output functions. One output function simply inserts a new line char after every output and the other one ...
Jose Fernando Lopez Fernandez's user avatar
4 votes
2 answers
175 views

Templated string formatting

This function formats a string by replacing every occurrence of '{n}' with the corresponding argument that was passed to the function where n is an index that ...
lelgetrekt's user avatar
4 votes
2 answers
193 views

Transpose types variadicly

I want to do template metaprogramming to compute the conversion from std::variant<Ts...> to ...
Tom Huntington's user avatar
4 votes
1 answer
1k views

Selecting variable amount of vector elements in C++

Suppose we are given a C++ vector. We want to specify a variable amount of indices and select elements from a vector being indexed. I have two implementation: (A) one relies on C++11 initializer lists,...
coderodde's user avatar
  • 28.2k
4 votes
1 answer
2k views

Event handler using variadic templates

I am currently working on a game and found myself in need of an event handler. I wrote an event handler similar to this one some time ago, but decided to update it using variadic templates (this is ...
Patrick Purcell's user avatar
4 votes
1 answer
288 views

Variadic Datablocks

I would like to have my variadic template class reviewed. First some explanation what it should do: I'm writing an application where I read in blocks of data. I know before i read in the data which ...
Sandro4912's user avatar
  • 3,066
4 votes
1 answer
114 views

Sleeping and invoking a task in Python 2.X and Python 3.X

I'm writing a program that needs to be compatible for both Python 2.X and Python 3.X. My problem is that, because of the differences between these two versions, I'm duplicating code. ...
tulians's user avatar
  • 379