noreturn c++ causes an error

Table of contents

Function introduction:

Error reporting example 1, added return value

Error example 2, without adding infinite loop:


Function introduction:

[[noreturn]] is a new feature introduced in C++11. It is a function attribute indicating that the function does not return. This means that once the function is called, it is not returned to the caller. Common non-returning functions include  exit()abort() etc.

For example:

[[noreturn]] void my_function() {
    // ... do some stuff ...
    exit(1); // This function will not return to the caller
}

In this example, my_function is marked as  [[noreturn]], which means the compiler knows that this function will not return to the caller. This helps the compiler optimize the code and can check for errors in the code. [[noreturn]] For example, if you write code after a function that is marked as  , the compiler may issue a warning because that code will never be executed.

If you get an error at the end of a function marked as  [[noreturn]] , it's probably because your function is actually trying to return to the caller, but  [[noreturn]] the compiler doesn't allow it because of the markup . You need to check your function to make sure it really doesn't return, for example, it might call  exit()abort(), or go into an infinite loop. If your function might actually return, then you shouldn't use  [[noreturn]] markers.

Error reporting example 1, added return value

#include <iostream>

[[noreturn]] int test() {
    std::cout << "This function should not return, but it does.\n";
    return 0;
}

int main() {
    test();
    std::cout << "Back in main.\n";
    return 0;
}

Error example 2, without adding infinite loop:

If your function is marked as such  but does not call other  functions  [[noreturn]]at the end of the function  (e.g.  etc.) or does not enter an infinite loop, then the compiler may generate an error or warning. This is because at the end of the function, control flow attempts to return to the caller, which   contradicts the declaration of .[[noreturn]]exit()abort()[[noreturn]]

For example, look at the following code:

#include <iostream>

[[noreturn]] void test() {
    std::cout << "This function should not return, but it does.\n";
    // No return statement, no call to another [[noreturn]] function, no infinite loop
}

int main() {
    test();
    std::cout << "Back in main.\n";
    return 0;
}

In this example, test the function is marked  [[noreturn]], but it does not call other  [[noreturn]] functions, nor does it enter an infinite loop, so at the end of the function, control flow attempts to return to the caller. Therefore, the compiler may generate an error or warning because  test the function's behavior  [[noreturn]] is inconsistent with its declaration.

If you want a function to actually never return, you need to make sure it calls a  [[noreturn]] function at the end, or enters an infinite loop. For example:

#include <iostream>
#include <cstdlib> // for exit()

[[noreturn]] void test() {
    std::cout << "This function will not return.\n";
    exit(0); // This is a [[noreturn]] function
}

int main() {
    test();
    std::cout << "Back in main.\n"; // This will never be printed
    return 0;
}

In this example, test the function is called at the end  exit(0), which is a  [[noreturn]] function, so  test the function really doesn't return to the caller.

Guess you like

Origin blog.csdn.net/jacke121/article/details/132925141