http://gcc.gnu.org/bugzilla/show_bug.cgi?id=49559
Summary: stable_sort calls self-move-assignment operator
Product: gcc
Version: 4.6.1
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: libstdc++
AssignedTo: [email protected]
ReportedBy: [email protected]
$ cat t.cc
#include <cassert>
#include <algorithm>
#include <iostream>
struct MyMoveClass
{
int val_;
explicit MyMoveClass( int val = 0 )
: val_( val )
{
std::cout << "ctr this=" << this << std::endl;
}
MyMoveClass( MyMoveClass const& rhs )
: val_( rhs.val_ )
{
std::cout << "ctr copy this=" << this << " rhs=" << &rhs << std::endl;
}
MyMoveClass( MyMoveClass && rhs )
: val_( rhs.val_ )
{
std::cout << "ctr move this=" << this << " rhs=" << &rhs << std::endl;
rhs.val_ = 0;
}
MyMoveClass& operator=( MyMoveClass && rhs )
{
std::cout << "assign move this=" << this << " rhs=" << &rhs << std::endl;
assert( this != &rhs );
val_ = rhs.val_;
rhs.val_ = 0;
return *this;
}
MyMoveClass& operator=( MyMoveClass const& rhs )
{
std::cout << "assign copy this=" << this << " rhs=" << &rhs << std::endl;
val_ = rhs.val_;
return *this;
}
~MyMoveClass()
{
std::cout << "dtr this=" << this << std::endl;
}
bool operator<( MyMoveClass const& rhs ) const
{
return val_ < rhs.val_;
}
};
int main()
{
MyMoveClass v(5);
std::stable_sort( &v, &v+1 );
return 0;
}
$ g++ -std=gnu++0x -o t t.cc
$ ./t
ctr this=0xbfe1730c
ctr move this=0x8afd008 rhs=0xbfe1730c
assign move this=0xbfe1730c rhs=0x8afd008
assign move this=0xbfe1730c rhs=0xbfe1730c
template: template.cc:31: MyMoveClass& MyMoveClass::operator=(MyMoveClass&&):
Assertion `this != &rhs' failed.
>From DR 1204: "Additionally this clarifies that move assignment operators need
not perform the traditional if (this != &rhs) test commonly found (and needed)
in copy assignment operators."
Note that std::sort() calls no copy constructor or assignment operator at all.
Seems sensible when there is only one element.