No return value in assignment operator and no error

Mateusz Łoskot mateusz@loskot.net
Sun Mar 5 17:18:00 GMT 2006


Hi,

I'm not sure if this is the right place to write about it, so if it 
isn't please direct me to the right one. Thanks!


Here is a very simple class with incomplete definition of assignment 
operator:

class A
{
     int x_;
public:
     A() : x_(0) {}
     A& operator=(A const& rhs)
     {
         x_ = rhs.x_;
     }
};


The question is why g++ 3.2.3 does not give any error message about 
missing return value?

VC++ 8.0 reports it as a bug:

Error 1 error C4716: 'A::operator=' : must return a value

Comeau reports it as a warning:

"ComeauTest.c", line 9: warning: missing return statement at end of non-void
           function "A::operator="
       }
       ^


g++ reports it only with -Wall switch on:

[mloskot@wl ~/tmp]$ g++ -g -Wall -ansi -pedantic assign.cpp
assign.cpp: In member function `A& A::operator=(const A&)':
assign.cpp:9: warning: no return statement in function returning non-void
assign.cpp:9: warning: control reaches end of non-void function


Please, could anyone tell me which compiler is right?
Should it be reported as error or just warning?

Best regards
-- 
Mateusz Łoskot
http://mateusz.loskot.net



More information about the Libstdc++ mailing list