exit() to end a function

bearophile bearophileHUGS at lycos.com
Sat Feb 12 14:03:09 PST 2011


A small D2 function:

import std.c.stdlib: exit;
int foo(int x) {
    if (x > 0)
        return x;
    exit(0);
    //assert(0);
}
void main() {}


DMD 2.051 gives this compile-time error:
test.d(2): Error: function test4.foo no return exp; or assert(0); at end of function

If I comment out the the final exit(0) and uncomment the assert(0), the code compiles correctly.
Is it meaningful and good to give std.c.stdlib.exit the same function-end semantics of assert(0)?

GCC has a "noreturn" function attribute that allows to tell the compiler what functions (beside few default ones like exit) don't return:
http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html#index-g_t_0040code_007bnoreturn_007d-function-attribute-2464

Bye,
bearophile


More information about the Digitalmars-d-learn mailing list