Dr. Gut
Dr. Gut

Reputation: 2846

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:

  1. UnusedDeclaration(): It is an unused function declaration with internal linkage. So I should get a warning.
  2. UnusedDefinition(): It is an unused function definition with internal linkage. So I should get a warning.
  3. 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.

Upvotes: 9

Views: 5999

Answers (1)

Jerry Coffin
Jerry Coffin

Reputation: 490408

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.

Upvotes: 4

Related Questions