Howard Hinnant, a C++ standard library designer, writes about C++ date algorithms, allocators, coding guidelines, and standard library containers.
HN commenters cite it for the stack allocator, the container comparison page, and the date library, treating it as an authoritative C++ source.
I'm bored of all the Rust vs. C++ comparisons, when the C++ code is something you simply don't write with modern and idiomatic C++11 and C++14. Not that you could not write it, but seeing this would not pass me reviewing the code.
The add_one example, as simple as it is, is a horrible example.
Why not embrace value semantics and write it like:
int add_one(int x) { return x + 1; }
There is simply no need for pointers or references. Write simple code first. To the heap allocation example:
auto x = std::make_unique<int>(5);
Even if this is a silly example, just say no to unnecessary heap allocation. Why not:
int x{5};
Now to the lifted example:
void add_one(std::unique_ptr<int> num) {
* num += 1;
}
This mixes operations with lifetime management. The add_one function does not need to care about ownership. And it does not need to take ownership of its argument. Instead on the call site do:
int add_one(int); // from above
auto p = make_unique(5);
auto six = add_one(*p); // <-
There are valid points in the blog post and I know that most snippets are meant as examples. But as someone writing C++14 you can't win me with those kinds of posts, even though I'm having a fun time playing with Rust in my spare time.
For more about modern practices see: http://klmr.me/slides/modern-cpp/
Also see Howard Hinnant's unique_ptr tutorial: http://howardhinnant.github.io/unique_ptr03.html
C++ has way way better ways to handle this. What you do instead is a std::vector with an allocator that has a fixed-size chunk of memory to cover your common, small case and falls back to malloc if the size becomes too large to fit into the internal allocation.
Then your VLA is stack allocated when small and heap allocated when large and works everywhere, even as a return value or other cases where alloca would fail.
Example: https://howardhinnant.github.io/stack_alloc.html
I don't think that is what is going on, this is invoking the constructor for the weekday class with the "modern" uniform initilization syntax. This solves the old C++ most vexing parse problem.
https://en.wikipedia.org/wiki/Most_vexing_parse
It seems like this is the C++ class in question:
https://howardhinnant.github.io/date/iso_week.html
Having only worked with gcc and clang, OP’s code looked completely fine to me and I was baffled why many comments think the code is at fault. Judging by this page [1], I agree this is entirely MSVC’s doing.
[1] http://howardhinnant.github.io/container_summary.html
here is an example of one from howard-hinnant: http://howardhinnant.github.io/stack_alloc.html
If we're doing thunderdome, JS doesn't beat C++: https://howardhinnant.github.io/smf.jpg