Home > Enterprise >  C : [[maybe_noreturn]]
C : [[maybe_noreturn]]

Time:02-08

As far as I am aware, there is no [[maybe_noreturn]] attribute in C .

I have a function which looks like this:

void func() {
    try {
        do_something();
    } catch (const std::exception& e) {
        std::exit(1);
    }
}

If I would mark func as [[noreturn]] I'd run into UB for the happy case. Is it UB when I do not return from a function that is not marked as [[noreturn]]?

Or are there other language constructs, compiler extensions or libraries that implement something like [[maybe_noreturn]]?

CodePudding user response:

Is it UB when I do not return from a function that is not marked as [[noreturn]]?

No, it's well defined to terminate the program from a function regardless whether it has [[noreturn]] attribute or not. Using [[noreturn]] is optional when it is appropriate - i.e. when a function terminates unconditionally - but it is always recommended in such cases.

No such attribute as [[maybe_noreturn]] is needed. If it were necessary, then you would find that you would have to declare any function potentially calling [[maybe_noreturn]] function to also be [[maybe_noreturn]], and then functions that call those functions, all the way to the top of the call chain.

  •  Tags:  
  • Related