-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRvoDisabled_3.cpp
51 lines (38 loc) · 1.28 KB
/
RvoDisabled_3.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
/**
* \file RvoDisabled
* \brief When RVO doesn’t / can’t happen
*
* \see https://shaharmike.com/cpp/rvo/#when-rvo-doesn-t-can-t-happen
*
* Returning by std::move()
*
* Returning by calling std::move() on the return value is an anti-pattern.
* It is wrong most of the times. It will indeed attempt to force move-constructor,
* but in doing so it will disable RVO. It is also redundant, as move will happen
* if it can even without explicitly calling std::move()
*/
#include <StdStream/StdStream.h>
#include <StdTest/StdTest.h>
#include <Stl.h>
#define RULE_5_OPTION_LOG 1
#include <Idioms/RuleOf/Rule5.h>
//--------------------------------------------------------------------------------------------------
Rule5
createObj()
{
Rule5 obj;
// error: moving a local object in a return statement prevents copy elision [-Werror=pessimizing-move]
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wpessimizing-move"
return std::move(obj);
#pragma GCC diagnostic pop
}
//--------------------------------------------------------------------------------------------------
int main(int, char **)
{
Rule5 obj = ::createObj();
return EXIT_SUCCESS;
}
//--------------------------------------------------------------------------------------------------
#if OUTPUT
#endif