简体   繁体   English

是否允许C ++编译器优化未引用的本地对象

[英]Is C++ compiler allowed to optimize out unreferenced local objects

I use following class to automatically set waiting cursor in the beginning of a certain function and reset the cursor when function returns. 我使用以下类在某个函数的开头自动设置等待光标,并在函数返回时重置光标。

class WaitCursorSetter
{
public:
    WaitCursorSetter() {QApplication::setOverrideCursor(Qt::WaitCursor);}
    virtual ~WaitCursorSetter() {QApplication::restoreOverrideCursor();}
};

I create a local WaitCursorSetter object when function begins. 我在函数开始时创建一个本地WaitCursorSetter对象。 Since waiting cursor is reset in the destructor of object, I do not have to reset the cursor before each and every return statement in the method since destructor gets called when function returns and object goes out of scope. 由于等待游标在对象的析构函数中被重置,因此当函数返回并且对象超出范围时,我不必在方法中的每个return语句之前重置游标,因为析构函数被调用。

If the compiler optimized out the unreferenced WaitCursorSetter object, this will not work. 如果编译器优化了未引用的WaitCursorSetter对象,则不起作用。 My problem is, is the compiler allowed to optimize out this object? 我的问题是,编译器是否允许优化此对象?

The compiler is not allowed to optimize away an automatic object whose destructors or initlization has side effects, we can see this by going to the draft standard section 3.7.3 : 编译器不允许优化掉析构函数或initlization有副作用的自动对象,我们可以通过转到草案标准部分3.7.3来看到这一点:

If a variable with automatic storage duration has initialization or a destructor with side effects, it shall not be destroyed before the end of its block, nor shall it be eliminated as an optimization even if it appears to be unused, except that a class object or its copy/move may be eliminated as specified in 12.8. 如果具有自动存储持续时间的变量具有初始化或具有副作用的析构函数,则不应在其块结束之前销毁它,也不应将其作为优化消除,即使它看起来是未使用的,除了类对象或其复制/移动可以按照12.8的规定予以删除。

It's perfectly safe to do that. 这样做是完全安全的。 In fact, it is an often used technique when putting in practice RAII. 事实上,在实践RAII时,它是一种经常使用的技术。 The compiler will not optimize out any local variable which has non-trivial constructor or destructor. 编译器不会优化任何具有非平凡构造函数或析构函数的局部变量。 Check out What is a non-trivial constructor in C++ . 看看C ++中什么是非平凡的构造函数

To also avoid the compiler warning regarding unused local variables, you can use Q_UNUSED macro. 要避免编译器警告有关未使用的局部变量,可以使用Q_UNUSED宏。

If you can observe any different the the compiler is not allowed to remove the object. 如果您可以观察到任何不同的编译器,则不允许删除该对象。

In this case the constructor/destructor have side effects, so the compiler will not remove them. 在这种情况下,构造函数/析构函数具有副作用,因此编译器不会删除它们。

The idea of delegating effects to construction/destruction of local stack-based objects is used often; 经常使用将效果委托给构造/销毁本地基于堆栈的对象的想法; for example: 例如:

{
    Locker L(my_lock);
    ...
}

This way the code in ... will be executing with a lock being held, and the lock will be automatically released when you leave the scope for any reason (just getting out of the block, executing a return or and if exception is thrown while inside). 这样, ...的代码将在保持锁定的情况下执行,并且当您因任何原因离开作用域时,锁定将自动释放(只是离开块,执行return或者如果抛出异常,内)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 有没有一种方法可以强制c ++编译器不优化静态库中的特定静态对象? - Is there a way to force c++ compiler to not optimize out specific static objects in a static library? 允许编译器优化掉局部volatile变量吗? - Is it allowed for a compiler to optimize away a local volatile variable? 是否允许编译器优化堆内存分配? - Is the compiler allowed to optimize out heap memory allocations? Visual C ++编译器是否优化未定义的宏块? - Does the Visual C++ compiler optimize out undefined macro blocks? C ++编译器是否会优化未使用的#include? - Does C++ compiler optimize out #includes that are not used? C++ 编译器是否优化了已知语句? - C++ Does the compiler optimize out known statements? 符合标准的C ++编译器是否允许在无符号整数上优化&lt;= 0的分支? - Is a standards conforming C++ compiler allowed to optimize away branching on <= 0 for unsigned integers? 何时允许编译器优化 C++ 中枚举或枚举类类型值的有效性检查? - When is the compiler allowed to optimize away a validity check of an enum or enum class type value in C++? 为什么我会收到“未引用的本地变量”警告? (C ++) - Why do I get an “Unreferenced Local Variable” warning? (C++) Visual Studio C ++编译器对局部变量对象的奇怪行为 - Visual Studio C++ Compiler strange behavior, on Local variable objects
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM