r/cpp • • Jul 04 '18

Be Mindful with Compiler-Generated Move Constructors

https://ibob.github.io/blog/2018/07/03/compiler-generated-move/
89 Upvotes

46 comments sorted by

View all comments

Show parent comment

2

u/matthieum Jul 04 '18

In reality, they're probably leveraging some kind of small buffer optimization, forcing moves to be nontrivial... so then it's just a QOI issue.

There is, at least in libstdc++ you should not have an allocation if your lambda only needs 8 bytes of storage.

Thus, you need to choose between:

  1. A noexcept move constructor for std::function,
  2. Small Buffer Optimization for 8 bytes items which cannot be moved.

At a glance, I'd pick (1) over (2) since I cannot think of such trivial objects which cannot be moved, so they don't seem very common, but I guess the committee had motivating examples.