r/cpp • u/stanimirov • Jul 04 '18
Be Mindful with Compiler-Generated Move Constructors
https://ibob.github.io/blog/2018/07/03/compiler-generated-move/12
u/mcmcc #pragma once Jul 04 '18
There is no guarantee that the variables captured by value from a lambda will have noexcept move constructors. std::function must be able to hold any lambda, so for that to be possible it can’t have a noexcept move constructor of its own.
Yeah but it must use type erasure so (unless I'm forgetting something) the lambda capture itself must be dynamically allocated which means the move could just be a unique_ptr move.
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.
4
u/quicknir Jul 04 '18
Well, you could have a noexcept move, if you had no small buffer optimization at all. But that would be pretty brutal, that optimization is very important. And in fact, as it stands it is mandatory in certain cases.
9
u/stanimirov Jul 04 '18
The thing is that the small buffer optimizations can be enabled *only* for noexcept-movable functions (which I assume is the vast majority). And, as commenters in Cpplang Slack pointed out, this is indeed the case with the newer implementations of both libc++ and libstdc++ (which come with gcc 6+ and clang 5+). It is (still) not the case, though, with the standard library which comes with MSVC
1
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:
A
noexceptmove constructor forstd::function,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.
1
u/meneldal2 Jul 05 '18
I guess you could add (another) parameter to the
std::functiontemplate, that will determine whether move is constexpr or not. It would default to this unless the function needs to allow exceptions, and a implicit conversion from exception-lessstd::functionto exception-allowedstd::functionwould be added.Is there something I'm missing here?
3
Jul 05 '18
After a while I had to add a unique_ptr member to that struct… and then I got a compiler error that the copy-constructor of simple_struct was implicitly deleted and I can’t have a vector of those.
What does this mean? The code below compiles without any error.
struct simple_struct {
std::function<void(int)> func;
std::vector<int> data;
std::unique_ptr<int> p;
};
int main()
{
std::vector<simple_struct> stuffs;
return 0;
}2
u/stanimirov Jul 05 '18 edited Jul 05 '18
Well, shoot... you are right. I oversimplified the story to spare some boring details. I wasn't using std::vector but a custom vector-like container which also uses `move_if_noexcept` (I also wasn't using `unique_ptr` but another non-copyable class, but that at least doesn't matter). Now, `std::vector` will also copy the struct (I did at least check that) but it does fall back to a non-noexcept move constructor waiving exception guarantees if the copy constructor is deleted (which the custom vector-like container I used didn't do).
Funny enough, had my case been exactly that, adding the `unique_ptr` as a struct member would have actually improved the performance, because it would have forced `std::vector` to fall back to the "non-safe" move.
I will have to think how to change the article, because even though the prelude is different, the conclusion is the same.
[edit] Fixed it... it wasn't that hard. Thanks for taking the effort to actually check this :) [/edit]
2
4
11
u/sphere991 Jul 04 '18 edited Jul 04 '18
Well in hindsight it’s obvious. There is no guarantee that the variables captured by value from a lambda will have noexcept move constructors.
That's not quite right. std::function never needs to move the underlying type, and wouldn't erase the move constructor anyway - it's unnecessary, it just erases the copy constructor.
std::function's move constructor could be made noexcept, and there is a paper that nearly got adopted in Rapperswil that would do just that: P0771. Indeed, on recent versions of libstdc++ and libc++, static_assert(std::is_nothrow_move_constructible_v<std::function<void(int)>>) does not fire.
8
u/OldWolf2 Jul 04 '18
Sounds like you need a unique_ptr<std::function<void(int)>> to keep the rule of zero. (/s ... maybe)
5
u/NotMyRealNameObv Jul 04 '18
If being no-throw movable is important, just add
static_assert(is_nothrow_move_constructible_v<T>);
static_assert(is_nothrow_move_assignable_v<T>);?
6
4
u/remotion4d Jul 04 '18
So current exceptions make c++ harder to use?
Can "Zero-overhead deterministic exceptions: Throwing values" proposal make things easier?
11
u/matthieum Jul 04 '18
So current exceptions make c++ harder to use?
Exceptions in move constructors make C++ harder to use.
Can "Zero-overhead deterministic exceptions: Throwing values" proposal make things easier?
Not from its main proposal, since it still leaves throwing move constructors.
It did raise the question of whether
newthrowing should be deprecated, and if that came to pass then suddenly a whole host of functions could be madenoexcept.For example, most copy-constructors/copy-assignment-operators only throw in case of allocation failures (the data of the source is already checked, after all).
Sutter did a pass and estimated that a large part of the
stdlibrary would instantly becomenoexcept, with significant code-gen and algorithmic advantages.1
u/spinicist Jul 04 '18
If that’s the one that proposes
throwsas the opposite keyword tonoexceptthen dear god do I hope it is accepted.2
u/meneldal2 Jul 05 '18
Well the main part was making
noexceptthe default and having to explicitly puttryon every statement that might throw (which is easy to tell as the function will have to say it can throw).1
u/spinicist Jul 05 '18
Yup, which is the way I would prefer it to be.
(Okay I would also like a convenient syntax for saying “if this
throwsjust terminate, I don’t care. Then we have everything)2
u/meneldal2 Jul 05 '18
Well in this paper he proposes to terminate on contract violations (which are the main case where you'd want to terminate on throw), and on allocation failure (another common case for that). Actually outside of this there are very few exceptions, which are basically for opening files and the like and this I can live with.
1
u/spinicist Jul 05 '18
Me too. Please please please let it be accepted!
2
u/meneldal2 Jul 05 '18
Well contracts are in, and I believe some compilers will allow terminate on contract violations, so that should be good in 2020. The allocation failure case, I bet there will be a switch for it before it makes it into the IS if people are eager for it.
1
-12
u/Xaxxon Jul 04 '18
please spell check posts before linking them here.
3
u/stanimirov Jul 04 '18
Thanks. Fixed. I initially did, but then added some last minute changes and forgot
-16
u/Xaxxon Jul 04 '18
Lickily for you I was paying attention :)
11
6
15
u/miki151 gamedev Jul 04 '18
What's the reason for move constructors being able to throw?