0

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?

Community
  • 1
  • 1
Jonathan Mee
  • 37,899
  • 23
  • 129
  • 288

1 Answers1

1

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.

SergeyA
  • 61,605
  • 5
  • 78
  • 137
  • Missing return does _not_ make a program ill-formed. Reaching the end of that function without a return...does. Subtle difference but your function may throw an exception (for example) and that code be perfectly valid. – Adriano Repetti Apr 19 '16 at 14:03
  • @AdrianoRepetti, if there is a nitpicking, this is the one. I'd edit, but since the question is a dupe anyways, I see no reason to :) – SergeyA Apr 19 '16 at 14:04