1

In the following code:

Widget makeWidget()
{
    return Widget();
}

void foo(Widget widget)
{
   ...
}

foo(makeWidget());

the Widget object will be always constructed in-place (inside foo function), so no move construction takes place (at least with all compilers I've tried). What are simple examples of passing a temporary to a function by value in a way that move construction will actually take place (without explicitly moving, i.e. using std::move)?

rubix_addict
  • 1,811
  • 13
  • 27

2 Answers2

3

The criterion for elision in this case is:

when a temporary class object that has not been bound to a reference would be copied/moved to a class object with the same cv-unqualified type

So a simple way to prevent elision would be to bind it to a reference:

foo(std::move(makeWidget());
Mike Seymour
  • 249,747
  • 28
  • 448
  • 644
  • I had implicit moves on my mind, so no std::move. I've edited my original question. Sorry for not being specific enough. – rubix_addict Apr 08 '15 at 12:16
  • @rubix_addict: In that case, I've no idea what you're asking. If you pass a temporary by value to a function with the same parameter type, then the move can be elided. There are no examples of it not being elided. – Mike Seymour Apr 08 '15 at 12:38
0

Gcc has the flag -fno-elide-constructors which disables copy elision. With that option, both of the moves will be done in your example code.

eerorika
  • 232,697
  • 12
  • 197
  • 326