I was getting a weird error from gcc and can't figure out why. I made the following sample code to make the problem more clear. Basically, there is a certain class for which I make it a copy constructor and a copy assignment operator private to accidentally call them.
#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\n", other.th ); } const branch& operator=( branch&& other ) { printf( "called! other.th=%d\n", 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 does not work with gcc. In fact, gcc complains that "branch :: branch (const branch &) is private", which, as I understand it, should not be called.
The assignment operator works, since if I replaced the body of main () with
branch a; branch b; b = a;
It will compile and execute as expected.
Is this the correct gcc behavior? If so, what is wrong with the above code? Any suggestion is helpful to me. Thanks!
source share