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 ...
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 ...
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
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
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
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 ...
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 ...
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
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
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
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
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
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 ...
5
votes
Coalescing lvalue references to std-optionals
Well, if you don't want recursion, you have to fold instead.
...
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 - ...
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 ...
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 ...
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:
...
Only top scored, non community-wiki answers of a minimum length are eligible
Related Tags
variadic × 78c++ × 63
template-meta-programming × 23
template × 22
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
image × 2
combinatorics × 2
iterator × 2
classes × 2
windows × 2
assembly × 2
library × 2
bitwise × 2