I was receiving a strange error from gcc and cannot figure out why. I made the following example code to make the problem more clear. Basically, there is a class defined, for which I make its copy constructor and copy assignment operator private, to prevent calling them accidentally.
#include <vector>
#include <cstdio>
using std::vector;
class branch
{
public:
int th;
private:
branch( const branch& other );
const branch& operator=( const branch& other );
public:
branch() : th(0) {}
branch( branch&& other )
{
printf( "called! other.th=%d
", other.th );
}
const branch& operator=( branch&& other )
{
printf( "called! other.th=%d
", other.th );
return (*this);
}
};
int main()
{
vector<branch> v;
branch a;
v.push_back( std::move(a) );
return 0;
}
I expect this code to compile, but it fails with gcc. Actually gcc complains that
"branch::branch(const branch&) is private", which as I understand shouldn't be called.
The assignment operator works, since if I replace the body of main() with
branch a;
branch b;
b = a;
It will compile and run as expected.
Is this a correct behavior of gcc? If so, what's wrong with the above code?
Any suggestion is helpful to me. Thank you!
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…