Capturing perfectly-forwarded variable in lambda

Is it correct to capture the perfectly-forwarded mStuff variable with the &mStuff syntax? Yes, assuming that you don’t use this lambda outside doSomething. Your code captures mStuff per reference and will correctly forward it inside the lambda. For mStuff being a parameter pack it suffices to use a simple-capture with a pack-expansion: template <typename… T> … Read more

Is copying 2D arrays with “memcpy” technically undefined behaviour?

std::memcpy(arr_copy, arr, sizeof arr); (your example) is well-defined. std::memcpy(arr_copy, arr[0], sizeof arr);, on the other hand, causes undefined behavior (at least in C++; not entirely sure about C). Multidimensional arrays are 1D arrays of arrays. As far as I know, they don’t get much (if any) special treatment compared to true 1D arrays (i.e. arrays … Read more

Why does using the ternary operator to return a string generate considerably different code from returning in an equivalent if/else block?

The overarching difference here is that the first version is branchless. 16 isn’t the length of any string here (the longer one, with NUL, is only 15 bytes long); it’s an offset into the return object (whose address is passed in RDI to support RVO), used to indicate that the small-string optimization is in use … Read more

C++: Mysteriously huge speedup from keeping one operand in a register

It’s likely a combination of a longer dependency chain, along with Load Misprediction*. Longer Dependency Chain: First, we identify the critical dependency paths. Then we look at the instruction latencies provided by: http://www.agner.org/optimize/instruction_tables.pdf (page 117) In the unoptimized version, the critical dependency path is: addsd -72(%rbp), %xmm0 movsd %xmm0, -72(%rbp) Internally, it probably breaks up … Read more