public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug libstdc++/52745] New: GCC4.7 vector uses copy instead of move constructor
@ 2012-03-27 23:06 J.W.Rogers+gcc at gmail dot com
  2012-03-27 23:13 ` [Bug libstdc++/52745] " paolo.carlini at oracle dot com
                   ` (6 more replies)
  0 siblings, 7 replies; 8+ messages in thread
From: J.W.Rogers+gcc at gmail dot com @ 2012-03-27 23:06 UTC (permalink / raw)
  To: gcc-bugs

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=52745

             Bug #: 52745
           Summary: GCC4.7 vector uses copy instead of move constructor
    Classification: Unclassified
           Product: gcc
           Version: 4.7.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: libstdc++
        AssignedTo: unassigned@gcc.gnu.org
        ReportedBy: J.W.Rogers+gcc@gmail.com


Using GCC 4.7.0, vector seems to prefer copy rather than move when resizing.

The following program produces different (and I think incorrect) results under
GCC 4.7.0 vs GCC 4.6.1

#include <iostream>
#include <vector>

struct Stuff
{
    Stuff( ) { }
    Stuff( Stuff&& o ) { std::cout << "Move" << std::endl; }
    Stuff( const Stuff& o ) { std::cout << "Copy" << std::endl; }
};


int main( const int argc, const char** argv )
{

    std::vector< Stuff > stuff;
    std::cout << "1" << std::endl;
    stuff.push_back( Stuff() );
    std::cout << "2" << std::endl;
    stuff.push_back( Stuff() );
    std::cout << "3" << std::endl;
    stuff.push_back( Stuff() );
}

Here is the output under GCC 4.6.1

jonathan@grindserv:~$ g++-4.6.1 -std=c++0x test.cpp
jonathan@grindserv:~$ ./a.out
1
Move
2
Move
Move
3
Move
Move
Move


Here is the output under GCC 4.7.0

jonathan@grindserv:~$ g++-4.7.0 -std=c++0x test.cpp
jonathan@grindserv:~$ ./a.out
1
Move
2
Move
Copy
3
Move
Copy
Copy


^ permalink raw reply	[flat|nested] 8+ messages in thread

* [Bug libstdc++/52745] GCC4.7 vector uses copy instead of move constructor
  2012-03-27 23:06 [Bug libstdc++/52745] New: GCC4.7 vector uses copy instead of move constructor J.W.Rogers+gcc at gmail dot com
@ 2012-03-27 23:13 ` paolo.carlini at oracle dot com
  2012-03-27 23:20 ` J.W.Rogers+gcc at gmail dot com
                   ` (5 subsequent siblings)
  6 siblings, 0 replies; 8+ messages in thread
From: paolo.carlini at oracle dot com @ 2012-03-27 23:13 UTC (permalink / raw)
  To: gcc-bugs

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=52745

--- Comment #1 from Paolo Carlini <paolo.carlini at oracle dot com> 2012-03-27 23:06:17 UTC ---
But Stuff' move-constructor isn't known not to throw...


^ permalink raw reply	[flat|nested] 8+ messages in thread

* [Bug libstdc++/52745] GCC4.7 vector uses copy instead of move constructor
  2012-03-27 23:06 [Bug libstdc++/52745] New: GCC4.7 vector uses copy instead of move constructor J.W.Rogers+gcc at gmail dot com
  2012-03-27 23:13 ` [Bug libstdc++/52745] " paolo.carlini at oracle dot com
@ 2012-03-27 23:20 ` J.W.Rogers+gcc at gmail dot com
  2012-03-27 23:26 ` paolo.carlini at oracle dot com
                   ` (4 subsequent siblings)
  6 siblings, 0 replies; 8+ messages in thread
From: J.W.Rogers+gcc at gmail dot com @ 2012-03-27 23:20 UTC (permalink / raw)
  To: gcc-bugs

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=52745

--- Comment #2 from Jonathan Rogers <J.W.Rogers+gcc at gmail dot com> 2012-03-27 23:13:29 UTC ---
(In reply to comment #1)
> But Stuff' move-constructor isn't known not to throw...

Okay, so it is a new requirement for move constructors to be marked as nothrow
in order for vector to use them for relocation?


^ permalink raw reply	[flat|nested] 8+ messages in thread

* [Bug libstdc++/52745] GCC4.7 vector uses copy instead of move constructor
  2012-03-27 23:06 [Bug libstdc++/52745] New: GCC4.7 vector uses copy instead of move constructor J.W.Rogers+gcc at gmail dot com
  2012-03-27 23:13 ` [Bug libstdc++/52745] " paolo.carlini at oracle dot com
  2012-03-27 23:20 ` J.W.Rogers+gcc at gmail dot com
@ 2012-03-27 23:26 ` paolo.carlini at oracle dot com
  2012-03-27 23:30 ` J.W.Rogers+gcc at gmail dot com
                   ` (3 subsequent siblings)
  6 siblings, 0 replies; 8+ messages in thread
From: paolo.carlini at oracle dot com @ 2012-03-27 23:26 UTC (permalink / raw)
  To: gcc-bugs

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=52745

--- Comment #3 from Paolo Carlini <paolo.carlini at oracle dot com> 2012-03-27 23:21:59 UTC ---
Otherwise, if the move constructor by chance throws, the push_back cannot have
no effects, as required by 23.2.1/10. Actually the requirement holds for all
the containers, but only std::vector implements it in 4.7, the other containers
(I *think* only std::deque needs work) will follow.


^ permalink raw reply	[flat|nested] 8+ messages in thread

* [Bug libstdc++/52745] GCC4.7 vector uses copy instead of move constructor
  2012-03-27 23:06 [Bug libstdc++/52745] New: GCC4.7 vector uses copy instead of move constructor J.W.Rogers+gcc at gmail dot com
                   ` (2 preceding siblings ...)
  2012-03-27 23:26 ` paolo.carlini at oracle dot com
@ 2012-03-27 23:30 ` J.W.Rogers+gcc at gmail dot com
  2012-03-27 23:42 ` paolo.carlini at oracle dot com
                   ` (2 subsequent siblings)
  6 siblings, 0 replies; 8+ messages in thread
From: J.W.Rogers+gcc at gmail dot com @ 2012-03-27 23:30 UTC (permalink / raw)
  To: gcc-bugs

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=52745

--- Comment #4 from Jonathan Rogers <J.W.Rogers+gcc at gmail dot com> 2012-03-27 23:25:45 UTC ---
(In reply to comment #3)
> Otherwise, if the move constructor by chance throws, the push_back cannot have
> no effects, as required by 23.2.1/10. Actually the requirement holds for all
> the containers, but only std::vector implements it in 4.7, the other containers
> (I *think* only std::deque needs work) will follow.

In my example code, if you comment out the copy constructor (leaving only the
move constructor) then vector will use the (potentially throwing) move
constructor.

If what you say is the case, shouldn't vector refuse to use the move
constructor?


^ permalink raw reply	[flat|nested] 8+ messages in thread

* [Bug libstdc++/52745] GCC4.7 vector uses copy instead of move constructor
  2012-03-27 23:06 [Bug libstdc++/52745] New: GCC4.7 vector uses copy instead of move constructor J.W.Rogers+gcc at gmail dot com
                   ` (3 preceding siblings ...)
  2012-03-27 23:30 ` J.W.Rogers+gcc at gmail dot com
@ 2012-03-27 23:42 ` paolo.carlini at oracle dot com
  2012-03-28  0:12 ` paolo.carlini at oracle dot com
  2012-03-28  0:22 ` J.W.Rogers+gcc at gmail dot com
  6 siblings, 0 replies; 8+ messages in thread
From: paolo.carlini at oracle dot com @ 2012-03-27 23:42 UTC (permalink / raw)
  To: gcc-bugs

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=52745

--- Comment #5 from Paolo Carlini <paolo.carlini at oracle dot com> 2012-03-27 23:30:23 UTC ---
That's known, it's a design choice: in that case you are essentially back to
the unsafe 4.6 behavior. Look for 'move_if_noexcept'.


^ permalink raw reply	[flat|nested] 8+ messages in thread

* [Bug libstdc++/52745] GCC4.7 vector uses copy instead of move constructor
  2012-03-27 23:06 [Bug libstdc++/52745] New: GCC4.7 vector uses copy instead of move constructor J.W.Rogers+gcc at gmail dot com
                   ` (4 preceding siblings ...)
  2012-03-27 23:42 ` paolo.carlini at oracle dot com
@ 2012-03-28  0:12 ` paolo.carlini at oracle dot com
  2012-03-28  0:22 ` J.W.Rogers+gcc at gmail dot com
  6 siblings, 0 replies; 8+ messages in thread
From: paolo.carlini at oracle dot com @ 2012-03-28  0:12 UTC (permalink / raw)
  To: gcc-bugs

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=52745

--- Comment #6 from Paolo Carlini <paolo.carlini at oracle dot com> 2012-03-27 23:44:44 UTC ---
Essentially the leeway for the unsafe fallback in the case of Comment #4 is
provided by 23.3.6.5/1: "If an exception is thrown by the move constructor of a
non-CopyInsertable T, the effects are unspecified".

But really, historically, people simply decided to replace for reallocations
unconditional moves with move_if_noexcexpt.


^ permalink raw reply	[flat|nested] 8+ messages in thread

* [Bug libstdc++/52745] GCC4.7 vector uses copy instead of move constructor
  2012-03-27 23:06 [Bug libstdc++/52745] New: GCC4.7 vector uses copy instead of move constructor J.W.Rogers+gcc at gmail dot com
                   ` (5 preceding siblings ...)
  2012-03-28  0:12 ` paolo.carlini at oracle dot com
@ 2012-03-28  0:22 ` J.W.Rogers+gcc at gmail dot com
  6 siblings, 0 replies; 8+ messages in thread
From: J.W.Rogers+gcc at gmail dot com @ 2012-03-28  0:22 UTC (permalink / raw)
  To: gcc-bugs

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=52745

Jonathan Rogers <J.W.Rogers+gcc at gmail dot com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|UNCONFIRMED                 |RESOLVED
         Resolution|                            |INVALID

--- Comment #7 from Jonathan Rogers <J.W.Rogers+gcc at gmail dot com> 2012-03-28 00:11:41 UTC ---
Thanks Paolo. I understand the situation now.

Thanks for your time and sorry the erroneous report!


^ permalink raw reply	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2012-03-28  0:12 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2012-03-27 23:06 [Bug libstdc++/52745] New: GCC4.7 vector uses copy instead of move constructor J.W.Rogers+gcc at gmail dot com
2012-03-27 23:13 ` [Bug libstdc++/52745] " paolo.carlini at oracle dot com
2012-03-27 23:20 ` J.W.Rogers+gcc at gmail dot com
2012-03-27 23:26 ` paolo.carlini at oracle dot com
2012-03-27 23:30 ` J.W.Rogers+gcc at gmail dot com
2012-03-27 23:42 ` paolo.carlini at oracle dot com
2012-03-28  0:12 ` paolo.carlini at oracle dot com
2012-03-28  0:22 ` J.W.Rogers+gcc at gmail dot com

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).