Topic: Is no return at end of function a compile-time error?
Author: jamshid@ses.com (Jamshid Afshar)
Date: 30 Nov 1994 04:27:17 GMT Raw View
The ARM 6.6.3 says "Flowing off the end of a function is equivalent to
a return with no value; this is illegal in a value-returning
function". If they're "equivalent", that implies to me that it should
give a compile time error rather than just returning an undefined
value.
But Stroustrup himself uses the following example in CPL2 4.6.4:
int fac(int n) {
if (n>1) return n*fac(n-1);
else return 1;
}
Does that mean that a compiler must do flow analysis and allow a
function body that couldn't possibly flowing off the end? How
complicated does the analysis have to be? For example, does a
compiler have to allow:
int g = 0;
int foo() {
switch (g) {
case 4:
break;
default:
return 7;
};
if (g==4) // yeah, I know this is silly
return 7;
}
Does it make any difference if the function returns a simple type as
opposed to a class object with a constructor/destructor?
If a compiler doesn't *have* to give an error for cases that would
allow flowing off the end, *may* it? For example, may a (future)
ANSI/ISO C++ -conforming compiler refuse to compile:
string foo(int i) {
if (i<kMax) return "hello";
abort();
}
Thanks,
Jamshid Afshar
jamshid@ses.com