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.
14
votes
2answers
167 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 ...
12
votes
3answers
6k 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:
...
11
votes
1answer
1k views
Variadic function with restricted types
I am designing a class which (among other things) acts as a vertex in an undirected graph.
...
11
votes
1answer
233 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
...
8
votes
2answers
151 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. ...
8
votes
1answer
4k 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 ...
7
votes
3answers
3k views
Sample printf implementation
Here is my code for printf() implementation in C. Please share your thoughts for improvements.
...
7
votes
2answers
1k 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.
...
7
votes
1answer
57 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 ...
7
votes
1answer
351 views
Most elegant variadic functor
Question
Suppose we have two sorts of classes
an input class Input
defines a type result_type
defines ...
6
votes
1answer
433 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:
...
6
votes
1answer
48 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 ...
6
votes
1answer
131 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'...
5
votes
2answers
4k views
Bit packing and unpacking
I needed a variadic function to pack and unpack bits into integer types. This is my first attempt:
...
5
votes
1answer
133 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:
...
5
votes
2answers
92 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. ...
5
votes
1answer
227 views
Thread-Safe Variadic Printing Function
Printing to stdout is thread-safe in many systems when using printf or std::cout, but not in ...
4
votes
4answers
75 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:
...
4
votes
1answer
1k 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 ...
4
votes
3answers
821 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 ...
4
votes
1answer
215 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 ...
4
votes
1answer
224 views
Optimizing variadic template pack subsequence matching algorithm
I'm building a small MPL module in one of my utility libraries for fun and learning experience.
One of the problems I'm trying to solve is getting a list of all indices where a sequence of types ...
4
votes
1answer
937 views
Simple mathematical operations (add, sub, mul, div) in C++11 template
I made a simple script to implement basic mathematics operations by using variadic functions. I would like to know if my implementation is correct.
The code only works for Visual C++ compiler Nov ...
4
votes
1answer
56 views
Invocation list implementation (signals/slots)
This class is mostly an educational exercise for me using some C++11 constructs. I wanted to create something similar to an "invocation list" in C#, i.e., a list of zero or more function objects which ...
3
votes
1answer
450 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,...
3
votes
2answers
96 views
3
votes
0answers
179 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 ...
3
votes
0answers
117 views
Invoke callable object passing arguments in “groups of N” using C++17 fold expressions
Interactive Wandbox example
Inspired by this Stack Oveflow question, I've implemented a function that, when called with a desired arity, a callable object, and a variadic amount of arguments, calls ...
2
votes
2answers
78 views
Simple JavaScript precondition checking implementation
I am writing a JavaScript application and would like to validate function arguments in many places. Most of these checks will be for correct argument types, or numeric values within specific ranges.
...
2
votes
2answers
119 views
A function for convenient string concatenation with variadic templates
I wrote this function for string concatenation some time ago.
Jo() stands for "Join". The name is shortened on purpose, because this function is used very often.
...
1
vote
2answers
100 views
Variadic average function in C++
The following code was inspired from a tutorial I found online:
...
1
vote
1answer
264 views
C-style va_args replacement
I inherited a lot of C code with many ellipsis (variadic) functions.
I have a lots of API with the following signature:
...
1
vote
0answers
50 views
Creating class method using macro to accept Visitator for each of their fields
I had a lot of classes that used a macro with listed fields. The macro was used to serialization, more like MSGPACK_DEFINE. I need a substitution of this mechanism, ...
0
votes
0answers
116 views
Function with default parameters in any order
Intent
Increase code usability/readability of functions with many default parameters while maintaining as much compile time safety as possible.
Motivation
It is very common to create functions that ...