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.
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.-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?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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With