Summary: | c++: Fail to build when <stdnoreturn.h> and the noreturn keyword is used | ||
---|---|---|---|
Product: | gcc | Reporter: | pere |
Component: | c++ | Assignee: | Not yet assigned to anyone <unassigned> |
Status: | RESOLVED INVALID | ||
Severity: | normal | CC: | jakub |
Priority: | P3 | ||
Version: | 8.2.1 | ||
Target Milestone: | --- | ||
Host: | Target: | ||
Build: | Known to work: | ||
Known to fail: | Last reconfirmed: |
Description
pere
2019-02-07 07:05:53 UTC
>stdnoreturn.h> is a C header, not part of C++, and not supported in C++.
The equivalent feature in C++ is an attribute:
[[noreturn]] void stop_now(int i);
Aha. I thought valid C code would also be valid C++ code, and this belief became stronger when clang (and clang++) would accept the code. Is there a list of C headers not supported by C++? The standard C++ headers are listed at: https://en.cppreference.com/w/cpp/header It does have an "Unsupported C headers" section: https://en.cppreference.com/w/cpp/header#Unsupported_C_headers "The C headers <stdatomic.h>, <stdnoreturn.h>, and <threads.h> are not included in C++ and have no cxxx equivalents." The reason you get a warning from cc (and clang) is that your function sometimes returns, which would lead to undefined behaviour. You are misusing noreturn. The noreturn specifier/attribute means the function *never* returns, it is not to be used for functions which *sometimes* don't return. Any function is allowed to call exit(), you're not supposed to mark such functions noreturn. The C11 standard says: A function declared with a _Noreturn function specifier shall not return to its caller. Recommended practice The implementation should produce a diagnostic message for a function declared with a _Noreturn function specifier that appears to be capable of returning to its caller. Thank you for the explanation. It is a lot clearer to me now. One thing confuses me, though. If <stdnoreturn.h> is not acceptable in C++, why is there no warning from the compiler when it is included, instead of giving the strange complain when 'noreturn' is used later in the file. The header doesn't define the noreturn macro (and must not, because one needs to be able to e.g. use [[noreturn]] and if noreturn was say a macro defined to [[noreturn]], that wouldn't work very well). I don't see why you are looking for a warning, you got an error that is fairly readable when you try to use it as if it were a macro. I just think it would have been useful if the compiler said something like 'header file <stdnoreturn.h> is not defined/available in C++' or something like that. It would provide feedback to the developer even if 'noreturn' is not used anywhere in the code to cause the error I ran into. Anywya, just an idea for you to consider, perhaps a feature request for a different bug report. :) But you can include it in C++, because the header exists. If you just include it an do nothing, there's no problem (it's not part of the C++ standard and so your program is not portable, conforming C++, but it compiles). The problem is that you tried to use the 'noreturn' specifier in your function, and that is not valid in C++. Including a useless header that doesn't define anything in C++ is fairly harmless. Writing invalid C++ code is invalid, and that's what triggers an error. The problem is not the header, it's your code. |