Skip to main content
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,265
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
11 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
  • 568
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
  • 81.7k
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
  • 61.7k
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.9k
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). ...
Loki Astari's user avatar
  • 96.6k
8 votes
Accepted

Partial application in C++

Looks like you are trying to re-invent std::bind. ...
Loki Astari's user avatar
  • 96.6k
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
  • 81.7k
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,605
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,572
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.4k
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.6k
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 ...
Loki Astari's user avatar
  • 96.6k
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,767
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,767
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,589
6 votes
Accepted

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

Allow 1D images One-dimensional images are not that uncommon in computer graphics. There is no reason why your class couldn't be made to support them. Make the constructors generic Instead of having 4 ...
G. Sliepen's user avatar
  • 61.7k
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,281
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
  • 988
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,767
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.4k
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.7k
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,767
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,589
5 votes

Coalescing lvalue references to std-optionals

Well, if you don't want recursion, you have to fold instead. ...
Deduplicator's user avatar
  • 19.3k
4 votes

Variadic template class that stores a vector of each type given

A few points of criticism of your approach: Prefer protected to private This isn't really about your overall design, but - ...
einpoklum's user avatar
  • 2,035
4 votes

Typesafe scanf-like function with variadic templates

So here's what I'm hoping to find out: Is this code idiomatic modern C++? Pretty much. You use recursion to handle the multiple arguments. But a better technique may be to use the compiler to build ...
Loki Astari's user avatar
  • 96.6k
4 votes
Accepted

Generic message system using variadic templates

Clearly define the use case When I think of "message systems", it's either message passing between different computers, processes, or threads. However, it seems that your message system only works ...
G. Sliepen's user avatar
  • 61.7k
4 votes
Accepted

Custom variadic min/max/sum functions

Is it intended? It really is difficult to see what you're trying to achieve here. Your code prevents some basic usage of a max function, for instance: ...
papagaga's user avatar
  • 5,767

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