且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

为什么没有gcc / g ++警告未使用的临时?

更新时间:2022-06-22 01:06:51

编译器不发出警告,因为很可能更新构造函数中的一些 static-member / global 变量(这是有效和有意义的)。例如:

Compiler doesn't issue a warning, because it's quite possible that you might be updating some static-member / global variable inside the constructor (which is valid and meaningful). e.g.:

struct A
{
  static int count;
  A () { count ++; }
};

现在当你简单地调用一个临时:

Now when you simply invoke a temporary:

A();

如果这样的更新没有发生,编译器不会挖入 A ,并检查是否有用。它总是假定是一个有效的场景。有很多这样的情况可以指出与临时相关。

In case if such update is not happening, compiler will not dig into the constructor of A and check if something useful is happening. It always assumes to be a valid scenario. There are many such cases can be pointed out related to temporaries.