Warning: function 'F' is not needed and will not be emitted

Dr.*_*Gut 6 c++ clang

Here is a little program (live on godbolt):

static void UnusedDeclaration ();
static void UnusedDefinition () {}
static void Declaration ();

decltype (Declaration ())*  global;
Run Code Online (Sandbox Code Playgroud)

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
Run Code Online (Sandbox Code Playgroud)

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).
  • 我可以使用 关闭案例 3 的警告-Wunused -Wno-unneeded-internal-declaration,但我不知道我是否会丢失一些重要信息。也许在其他一些情况下,此警告很有用。第三个警告是在什么确切情况下出现的?

Jer*_*fin 4

没有对第一个函数的引用,因此基本上可以忽略它。

第二个函数被“调用”,但正如您所指出的,仅在未评估的上下文中。这意味着永远不会评估该调用。编译器确定函数被调用时将返回的类型,但仅此而已。由于它从未被实际调用,因此函数本身永远不需要或用作函数,因此编译器不会为其生成任何代码。

这些消息是不同的,因为即使最终效果相同,每个消息所达到的结果也略有不同,并且编译器试图为您提供可能有用的信息。