[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 14:36:23 +0000 |
> 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.
>
> #include <utility>
>
> struct T {
> void show () { }
> };
>
> T fn ()
> {
> T t1;
>
> // No warning from GCC here.
> T t2 = std::move (T ());
>
> t2.show ();
>
> // GCC does warn here.
> return std::move (t1);
> }
>
> jwe
>
Here's the results clang++ (Apple LLVM version 10.0.1 (clang-1001.0.46.4)):
------------------------------------------
## no -std option (defaults to gnu++14)
$ clang++ -Wall -c prova.cc
prova.cc:12:9: warning: moving a temporary object prevents copy elision
[-Wpessimizing-move]
T t2 = std::move (T ());
^
prova.cc:12:9: note: remove std::move call here
T t2 = std::move (T ());
^~~~~~~~~~~ ~
prova.cc:17:9: warning: moving a local object in a return statement prevents
copy elision [-Wpessimizing-move]
return std::move (t1);
^
prova.cc:17:9: note: remove std::move call here
return std::move (t1);
^~~~~~~~~~~ ~
2 warnings generated.
------------------------------------------
## -std=c++11
$ clang++ -std=c++11 -Wall -c prova.cc
prova.cc:12:9: warning: moving a temporary object prevents copy elision
[-Wpessimizing-move]
T t2 = std::move (T ());
^
prova.cc:12:9: note: remove std::move call here
T t2 = std::move (T ());
^~~~~~~~~~~ ~
prova.cc:17:9: warning: moving a local object in a return statement prevents
copy elision [-Wpessimizing-move]
return std::move (t1);
^
prova.cc:17:9: note: remove std::move call here
return std::move (t1);
^~~~~~~~~~~ ~
2 warnings generated.
------------------------------------------
## -std=c++14
$ clang++ -std=c++14 -Wall -c prova.cc
prova.cc:12:9: warning: moving a temporary object prevents copy elision
[-Wpessimizing-move]
T t2 = std::move (T ());
^
prova.cc:12:9: note: remove std::move call here
T t2 = std::move (T ());
^~~~~~~~~~~ ~
prova.cc:17:9: warning: moving a local object in a return statement prevents
copy elision [-Wpessimizing-move]
return std::move (t1);
^
prova.cc:17:9: note: remove std::move call here
return std::move (t1);
^~~~~~~~~~~ ~
2 warnings generated.
------------------------------------------
## -std=c++17
$ clang++ -std=c++17 -Wall -c prova.cc
prova.cc:12:9: warning: moving a temporary object prevents copy elision
[-Wpessimizing-move]
T t2 = std::move (T ());
^
prova.cc:12:9: note: remove std::move call here
T t2 = std::move (T ());
^~~~~~~~~~~ ~
prova.cc:17:9: warning: moving a local object in a return statement prevents
copy elision [-Wpessimizing-move]
return std::move (t1);
^
prova.cc:17:9: note: remove std::move call here
return std::move (t1);
^~~~~~~~~~~ ~
2 warnings generated.
------------------------------------------
the -std option does not seem to make any difference but it seems g++ and
clang++ do behave differently ...
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, 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 <=
- Re: move constructors likely a requirement, Carlo De Falco, 2019/09/05
- Re: move constructors likely a requirement, Carlo De Falco, 2019/09/05