Search code examples
c++operatorsreturn-valueimplicitassign

Assignment Operator Return


I was working on an example for this answer but I forgot to type return *this; to conclude my assignment operator, so Val's assignment operator looks like this:

Val& operator= (const int _a) {a = _a; b = _a + fmod(b, 1.0F);}

It seems like it shouldn't, but gcc compiles and runs this code. How is this possible? Is this like the main function where there's an implied return?


Solution

  • No, there is no implied return, but in default mode g++ does not report missing return as an error. The reason for this is that missing return makes a program ill-formed, and Standard does not require any reporting for ill-formed programs.

    You need to make sure you always have following arguments to your g++: -Wall -Wextra -Werror -pedantic. This will make sure gcc will report errors like that and much more.