'Warning: function 'F' is not needed and will not be emitted
Here is a little program (live on godbolt):
static void UnusedDeclaration();
static void UnusedDefinition() {}
static void Declaration();
decltype(Declaration())* global;
Ideally I would expect the following warnings, if I compile it with clang, -Wunused
:
UnusedDeclaration()
: It is an unused function declaration with internal linkage. So I should get a warning.UnusedDefinition()
: It is an unused function definition with internal linkage. So I should get a warning.Declaration()
: This declaration is used. So I should not get a warning.
Actually all three cases get a warning:
warning: unused function 'UnusedDeclaration'
warning: unused function 'UnusedDefinition'
warning: function 'Declaration' is not needed and will not be emitted
I have a problem with case 3. I think, the compiler should not warn me about anything, but it does.
- Why do I get a warning for case 3?
function 'Declaration' is not needed
– I honestly need that declaration. I use it in an unevaluated context, the compiler does not need to complain about it.and will not be emitted
– I am not sure what emitting means. I have written a separate question about it.
- Is there a combination of warning flags to pass to the compiler on the command line, to achieve what I expect (warning for case 1 and 2, no warning for case 3).
- I could turn off warning for case 3 with
-Wunused -Wno-unneeded-internal-declaration
, but I don't know if I lose some important information with it. Maybe in some other cases this warning is useful. In what exact situation does this 3rd warning come?
Solution 1:[1]
There's no reference to the first function, so it can be basically ignored.
The second function is "called", but as you noted, only in an unevaluated context. That means the call will never be evaluated. The compiler determines the type the function would return if it was called, but that's all. Since it's never actually called, the function itself is never needed or used as a function, so the compiler doesn't generate any code for it.
The messages are different because even though the final effect is the same, each reaches that result slightly differently, and the compiler is trying to give you the information that might be useful.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 |