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 ...
Snowhawk's user avatar
  • 6,148
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 ...
lubgr's user avatar
  • 956
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 ...
Caleth's user avatar
  • 559
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 ...
5gon12eder's user avatar
  • 4,260
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 ...
Toby Speight's user avatar
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 || ...); } ...
G. Sliepen's user avatar
  • 50.5k
9 votes

Coalescing lvalue references to std-optionals

Folding? Bah. New-fangled nonsense. Real programmers use for loops for for looping. ...
user673679's user avatar
  • 11.6k
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 ...
user2296177's user avatar
  • 3,533
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). ...
Martin York's user avatar
  • 94.3k
8 votes
Accepted

Partial application in C++

Looks like you are trying to re-invent std::bind. ...
Martin York's user avatar
  • 94.3k
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 ...
Toby Speight's user avatar
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 ...
L. F.'s user avatar
  • 9,456
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) ...
Mat's user avatar
  • 2,995
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 ...
Rakete1111's user avatar
  • 2,562
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 ...
Quuxplusone's user avatar
  • 19.3k
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 <...
Edward's user avatar
  • 65.3k
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 ...
JS1's user avatar
  • 28.5k
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 ...
Martin York's user avatar
  • 94.3k
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 ...
papagaga's user avatar
  • 5,717
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...
papagaga's user avatar
  • 5,717
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 ...
Incomputable's user avatar
  • 9,374
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 ...
mdfst13's user avatar
  • 21.4k
5 votes
Accepted

Variadic average function in C++

One possibility would be to use an std::initializer_list: ...
Jerry Coffin's user avatar
  • 33.5k
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 ...
chux - Reinstate Monica's user avatar
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: <...
papagaga's user avatar
  • 5,717
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 ...
bipll's user avatar
  • 968
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. ...
miscco's user avatar
  • 4,231
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 ...
Quuxplusone's user avatar
  • 19.3k
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 ...
papagaga's user avatar
  • 5,717
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 ...
Incomputable's user avatar
  • 9,374

Only top scored, non community-wiki answers of a minimum length are eligible