18
votes
Accepted
Min function accepting varying number of arguments in C++17
template <typename Less, typename T, typename... Ts>
constexpr const T& min(Less less, const T& a, const T& b, const Ts&... rems) {
This ...
12
votes
Min function accepting varying number of arguments in C++17
This looks nice! Two issues I see here,
When compiling your template with clang, it refuses the if constexpr dispatch for every ...
10
votes
Accepted
Variadic templated method to extract values from a table
Yes, it is a good example
Variadic templates are associated with recursion by similarity to functional programming, where an recursion is a simple method of operating on sequences. Direct expansion ...
9
votes
Accepted
Use va_list to format a string
Don't invent your own language through macros
GLOBAL and LOCAL might look cute but they make the code harder to read for ...
9
votes
Min function accepting varying number of arguments in C++17
It works well, according to my simple test program:
#include <functional>
int main()
{
return min(std::less<int>(), 2, 0, 3);
}
I suggest that when ...
9
votes
Accepted
Coalescing lvalue references to std-optionals
So ideally you want to write something like:
template <typename... Ts>
constepxr auto coalesce(std::optional<Ts>&... xs)
{
return (xs || ...);
}
...
9
votes
Coalescing lvalue references to std-optionals
Folding? Bah. New-fangled nonsense. Real programmers use for loops for for looping.
...
8
votes
Accepted
Concatenate two containers, e.g. vector or strings
Your current strategy will cause more reallocations than you need to, which is not efficient. You already have all of your vector parameters, so you simply need to get their size, and reserve that ...
8
votes
Accepted
Rotate a given number of variables in-place
Issues (for me)
I get a couple of errors when I try and compile.
This error I have a feeling is caused because you are using a more up to date compiler (I presume this is a C++17 feature).
...
8
votes
Accepted
8
votes
Accepted
Max function with variable number of arguments
I think this function is unnecessary.
We can deal with defective classes (that don't properly implement the standard LessThanComparable concept) by either fixing ...
8
votes
Accepted
`printf` improvement with C++ variadic templates
General design
Currently, your function is defined to have "undefined behavior" if the number of arguments is wrong. This is sub-optimal. Checking is trivial in this case, so report the problem in ...
7
votes
Formatted print without the need to specify type matching specifiers using _Generic
Technicality
Pedantically speaking, the GP_get_type function has undefined behavior if the pointer it gets isn't a pointer into (or one past the end of) ...
7
votes
The perfect function alias
Is there a situation where the compiler will not optimize away the wrapper because the wrapper and the original function have different semantics?
Yes to the first part and no to the second. Any ...
7
votes
Accepted
Meta functions for sequences of exponents of 2
Certainly your definitions of printing_type and print could be shortened. You're currently doing
...
6
votes
Accepted
Formatted print without the need to specify type matching specifiers using _Generic
I see some things that may help you improve your code.
Cleanly separate interface from implementation
The header file contains a variable and four inline functions. The variable should be declared <...
6
votes
Accepted
A variadic C function for concatenating multiple strings
Always return allocated string or never do it
This code here:
if (count < 1)
{
return "";
}
is bad because you return a static string whereas the normal ...
6
votes
Accepted
Header-only variadic console output and runtime input validation in C++ using templates
Comment
First question is why I would use this instead of the standard operator<< that we are all very familiar with?
Code Review
Reserved Identifiers
...
6
votes
Generators and Distributions 2.0
About the seed function, though, I must warn you that you might be surprised at some point by which overload the compiler will choose. Unless you really know what ...
6
votes
Apply a function to each element of a tuple ("map" a tuple)
transform, or map ?
I wouldn't name this function transform, because it's very different from that stl algorithm. std::transform...
6
votes
Apply a function to each element of a tuple ("map" a tuple)
Code review
I agree with @papagaga that the name could be better, though I would choose perform_mappping. I just obfuscated name ...
5
votes
A variadic C function for concatenating multiple strings
Automate consistency
p = malloc(sizeof(char*) * count);
len_array = calloc(count, sizeof(size_t));
If you instead say
...
5
votes
Accepted
Variadic average function in C++
One possibility would be to use an std::initializer_list:
...
5
votes
Accepted
Implementing checksum add without carry in C
It appears code is simply exclusive or-ing the arguments. Sounds like a "add without carry".
"Is my logic for the implementation correct?"
Appears correct except the range of acceptable values is ...
5
votes
Generically encapsulating random generators and distributions into a class template
I like your code. You can upgrade some bits though:
Scoped enums
It is better to use scoped enumerations. They're declared with a slightly different syntax:
<...
5
votes
Generically encapsulating random generators and distributions into a class template
A few observations.
Visibility
Is there any special plan in declaring Clock a public member type? It's not used in any other part of the interface. Currently its ...
5
votes
Generators and Distributions 2.0
Your seed with a defined function seems wrong. THe first thing is you are missing the braces around the if and only the first statement is inside the if.
...
5
votes
Alternative to integer_sequence trick
Looks good to me! I mean, I'm not likely to convert to it because of its heavyweightness — you noted its use of std::apply, but it also pulls in all of ...
5
votes
Variadic strcat for c++17
Benchmarking is a tricky business. I'll focus more on the code part, you tell me if you get an improvement on the performance part too.
Containers or c-style strings?
with containers like ...
5
votes
Accepted
Small Formatting Library
Don't make unnecessary type aliases
using string_t = std::string;
using CharT = string_t::value_type;
Those neither add readability nor make the existing ...
Only top scored, non community-wiki answers of a minimum length are eligible
Related Tags
variadic × 78c++ × 63
template-meta-programming × 23
template × 21
c++11 × 20
c++17 × 17
formatting × 9
c × 8
c++14 × 7
strings × 6
macros × 6
functional-programming × 3
reinventing-the-wheel × 3
c++20 × 3
python × 2
recursion × 2
random × 2
combinatorics × 2
iterator × 2
windows × 2
assembly × 2
library × 2
bitwise × 2
lambda × 2
ffi × 2