[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: move constructors likely a requirement
From: |
Carlo De Falco |
Subject: |
Re: move constructors likely a requirement |
Date: |
Thu, 5 Sep 2019 15:09:16 +0000 |
> Il giorno 5 set 2019, alle ore 17:04, Carlo de Falco <address@hidden> ha
> scritto:
>
>
>
>> Il giorno 5 set 2019, alle ore 16:36, Carlo De Falco <address@hidden> ha
>> scritto:
>>
>>
>>
>>> Il giorno 5 set 2019, alle ore 16:20, John W. Eaton <address@hidden> ha
>>> scritto:
>>>
>>> On 9/5/19 7:59 AM, Carlo De Falco wrote:
>>>>> Il giorno 4 set 2019, alle ore 20:14, John W. Eaton <address@hidden> ha
>>>>> scritto:
>>>>>
>>>>> If I understand correctly, -Wpessimizing-move is supposed to be implied
>>>>> when compiling C++ code with GCC and using -Wall.
>>>> If I understand correctly, copy elision is mandated by the standard only
>>>> since c++14, while we are using c++11.
>>>> Could it be possible that gcc and clang have different criteria to decide
>>>> whether to make the optimization?
>>>
>>> I compiled the following program with g++ 9 (the version that introduced
>>> the -Wpessimizing-move warning option) using both -std=c++17 and no -std
>>> option (defaults to c++14) and the warnings are the same in both cases.
> ....
>>
>> the -std option does not seem to make any difference but it seems g++ and
>> clang++ do behave differently ...
>>
>> c.
>
> If I compile and run the following
>
> #include <iostream>
>
> struct T {
> void show () { };
> T () { std::cout << "default constructor" << std::endl; }
> T (const T& a) { std::cout << "copy constructor" << __func__ << std::endl; }
> ~T () { std::cout << "destructor" << std::endl; }
> T (T&& a) { std::cout << "move constructor" << std::endl; }
> };
>
> T fn ()
> {
> T t1;
>
> // No warning from GCC here.
> T t2 = (T ());
>
> t2.show ();
>
> // GCC does warn here.
> return (t1);
> }
>
> int
> main ()
> {
> T b = fn ();
> return 0;
> }
>
> I get
>
> $ ./a.out
> default constructor
> default constructor
> destructor
> destructor
>
>
> Am I right to infer that copy elision is actually being performed?
> If I put back the std::move calls I get instead
>
> $ ./a.out
> default constructor
> default constructor
> move constructor
> destructor
> move constructor
> destructor
> destructor
> destructor
>
> c.
BTW, if I use g++ from gcc9 :
$ g++-mp-9 --version
g++-mp-9 (MacPorts gcc9 9.2.0_0) 9.2.0
Copyright (C) 2019 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
I see the same output as for clang++ when running the code but one less warning
when compiling
$ g++-mp-9 -Wall prova.cc
prova.cc: In function 'T fn()':
prova.cc:21:19: warning: moving a local object in a return statement prevents
copy elision [-Wpessimizing-move]
21 | return std::move (t1);
| ~~~~~~~~~~^~~~
prova.cc:21:19: note: remove 'std::move' call
c.
- Re: move constructors likely a requirement, (continued)
- Re: move constructors likely a requirement, John W. Eaton, 2019/09/05
- Re: move constructors likely a requirement, Carlo de Falco, 2019/09/05
- Re: move constructors likely a requirement, Carlo de Falco, 2019/09/05
- Re: move constructors likely a requirement, kingcrimson, 2019/09/06
- Re: move constructors likely a requirement, kingcrimson, 2019/09/06
- Re: move constructors likely a requirement, Carlo De Falco, 2019/09/06
- Re: move constructors likely a requirement, Carlo De Falco, 2019/09/05
- Re: move constructors likely a requirement, John W. Eaton, 2019/09/05
- Re: move constructors likely a requirement, Carlo De Falco, 2019/09/05
- Re: move constructors likely a requirement, Carlo De Falco, 2019/09/05
- Re: move constructors likely a requirement,
Carlo De Falco <=