Using gcc version 4.4.3 as follows:
gcc -g -x c++ -lstdc++ -std=c++98 -o ./main ./main.cpp
this code in main.cpp compiles fine:
#include <iostream>
struct A
{
A()
: m_flag(false)
{
}
const bool m_flag;
};
static A aa = A();
int main(int argc, char* argv[])
{
A a;
// Not static = copy OK
A b( a );
A c = b;
A d = A();
// Static = copy not OK
// aa = A();
}
But if I uncomment aa = A(); I get:
./main.cpp: In member function 'A& A::operator=(const A&)':
./main.cpp:4: error: non-static const member 'const bool A::m_flag', can't use default assignment operator
./main.cpp: In function 'int main(int, char**)':
./main.cpp:24: note: synthesized method 'A& A::operator=(const A&)' first required here
Why does the default copy construction and copy assignment work for copies on the stack but not when replacing the non-const static with a copy?
The problem is this line:
which invokes default
operator =. Modifyingconstmember is a trivial error.invokes default copy constructor (not
operator =as you assume), wherem_flagis assigned a new value at the initialization itself.