Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Lack of temporaries

34 views
Skip to first unread message

Kamil Laskowski

unread,
Dec 10, 2014, 4:15:01 PM12/10/14
to
I thought I understand fundamental rules of C++ until I wrote this simple program:

#include <iostream>
using namespace std;

#define exampleOne

class MyType
{
public:
MyType()
{
cout << "def ctor" << endl;
}
#ifdef exampleOne
MyType(const MyType& x)
{
cout << "copy ctor";
}
#endif

#ifdef exampleTwo
MyType(MyType&& x)
{
cout << "move ctor" << endl;
}
#endif

};

MyType fun()
{
MyType mt;
return mt;
}

int main()
{
MyType m = fun();
}

which includes two examples: one with move constructor and one with copy constructor. I compiled it with VC12 in Debug with disabled optimization and got only "def ctor copy ctor" for exampleOne and only "def ctor move ctor" for exampleTwo. It driving me mad because I was strongly convinced that *temporary* is created after returning from function fun and from this *temporary* proper object (m in main) is created.

Could you help me with understanding this?

Ian Collins

unread,
Dec 10, 2014, 4:33:35 PM12/10/14
to
Please wrap your lines!

Have a read up on RVO "Return Value Optimisation".

--
Ian Collins

bill...@gmail.com

unread,
Dec 10, 2014, 5:01:40 PM12/10/14
to
I am sorry for these lenghty lines, but I am not able to edit my previous post. BTW, why wrapping is not automatic?

Yes, I read and know about RVO but why I am getting this when I turned off any optimisation? When I saw the output I felt really concern about my knowledge and decided to ask people with more experience. Is there any way to compile this without RVO and get desired output?

Paavo Helde

unread,
Dec 10, 2014, 5:08:22 PM12/10/14
to
Kamil Laskowski <bill...@gmail.com> wrote in
news:ceb0c5f1-d7c0-4998...@googlegroups.com:
The standard indeed speaks about temporaries here, but this is just a
formal/logical description how the things happen. The compiler can
legally elide any number of copy/move constructor calls, regardless of
the optimization level. If the function body were more complicated, with
multiple named and unnamed return points then I guess there would be a
better chance to see temporaries (but not guaranteed).

In an optimized build also the remaining 'copy ctor' will be probably
optimized away.

Cheers
Paavo

Ian Collins

unread,
Dec 10, 2014, 5:11:38 PM12/10/14
to
bill...@gmail.com wrote:
> W dniu środa, 10 grudnia 2014 22:33:35 UTC+1 użytkownik Ian Collins napisał:
>> Kamil Laskowski wrote:

<snip>

>>>
>>> which includes two examples: one with move constructor and one with
>>> copy constructor. I compiled it with VC12 in Debug with disabled
>>> optimization and got only "def ctor copy ctor" for exampleOne and only
>>> "def ctor move ctor" for exampleTwo. It driving me mad because I was
>>> strongly convinced that *temporary* is created after returning from
>>> function fun and from this *temporary* proper object (m in main) is created.
>>>
>>> Could you help me with understanding this?
>>
>> Please wrap your lines!
>>
>> Have a read up on RVO "Return Value Optimisation".
>
> I am sorry for these lenghty lines, but I am not able to edit my
> previous post. BTW, why wrapping is not automatic?

Because the google Usenet interface is horrible... Just add a return
every 70-80 characters.

> Yes, I read and know about RVO but why I am getting this when I
> turned off any optimisation?

Probably because it is part of the language, not a compiler optimisation.

> When I saw the output I felt really concern about
> my knowledge and decided to ask people with more experience. Is there
> any way to compile this without RVO and get desired output?

That's a question for your compiler documentation.

--
Ian Collins

Paavo Helde

unread,
Dec 10, 2014, 5:22:27 PM12/10/14
to
bill...@gmail.com wrote in
news:e6a96d57-c79d-4290...@googlegroups.com:

> Is there any way to compile this without RVO and get desired output?

Probably not, there is not much demand for such compiler features which
would just make the generated code worse without any benefit whatsoever ;-)

Cheers
Paavo


Kamil Laskowski

unread,
Dec 10, 2014, 5:24:42 PM12/10/14
to
Thanks guys for your time!

Vincenzo Mercuri

unread,
Dec 10, 2014, 5:45:50 PM12/10/14
to
Il 10/12/2014 23:01, bill...@gmail.com ha scritto:
...
>
> Is there any way to compile this without RVO and get desired output?
>
...

Not in VC++. In GCC you have the flag "-fno-elide-constructors".

I get:

$>./test
def ctor
copy ctorcopy ctor
$>

for exampleOne, and:

$>./test
def ctor
move ctor
move ctor

$>

for exampleTwo.

--
Vincenzo Mercuri
0 new messages