r/cpp • u/Z01dbrg • Feb 12 '18
My Precious Compile Time Bool (long introduction to Move Semantics)
https://medium.com/@gaussnoise/my-precious-compile-time-bool-long-introduction-to-move-semantics-c9ee73c370c72
u/gracicot Feb 12 '18
Constructor should take by value and move arguments. Correct me if I'm wrong, but I think the values are elided and only moved or copied into the members directly.
Also, taking constructors arguments by value makes writing it much easier. If there's no performance impact, I wonder why I'd even bother writing that n2 set or using forwarding references.
1
u/Z01dbrg Feb 13 '18 edited Feb 13 '18
Correct me if I'm wrong, but I think the values are elided and only moved or copied into the members directly.
You are wrong in a sense that it is not guaranteed to happen. Article says when discussing pass by value constructor that optimizer may make difference between pass by value and universal references constructor nonexistent.
Also, taking constructors arguments by value makes writing it much easier. If there's no performance impact, I wonder why I'd even bother writing that n2 set or using forwarding references.
Article is long but it mentions the difference... When you have value argument in case of a temporary this happens:
temporary is moved to argument
argument is moved into member
Using universal references or n2 overloads your temporary does not get moved into the argument(since argument is a a reference). Member is constructed from reference to the temporary. So less moves(again optimizer may make this difference nonexistent).
Like article also says even if the optimizer does not optimize moves for string and vector are cheap so most of the time you should not bother to go for best solution since it is ugly.
2
6
u/koval4 Feb 12 '18
Emojis... Emojis everywhere... They're a bit annoying, so it becomes harder to read text. It's kinda minor problem compared with problem that was risen in article, but still, it kinda ruins it.