scoped_lock 如何避免发出“未使用的变量”?警告?
boost::mutex::scoped_lock
是一个围绕锁定互斥锁的方便的 RAII 包装器。我在其他方面使用了类似的技术:RAII 包装器要求数据接口与串行设备分离/重新连接。
但我不明白的是,为什么在下面的代码中只有我的对象 mst
— 其实例化和销毁确实有副作用 — 导致 g++< /code> 发出“未使用的变量”警告错误,而
l
设法保持沉默。
你知道吗?你能告诉我吗?
[generic@sentinel ~]$ cat test.cpp
#include <boost/shared_ptr.hpp>
#include <boost/thread/mutex.hpp>
#include <iostream>
struct MyScopedThing;
struct MyWorkerObject {
void a() { std::cout << "a"; }
void b() { std::cout << "b"; }
boost::shared_ptr<MyScopedThing> getScopedThing();
};
struct MyScopedThing {
MyScopedThing(MyWorkerObject& w) : w(w) {
w.a();
}
~MyScopedThing() {
w.b();
}
MyWorkerObject& w;
};
boost::shared_ptr<MyScopedThing> MyWorkerObject::getScopedThing() {
return boost::shared_ptr<MyScopedThing>(new MyScopedThing(*this));
}
int main() {
boost::mutex m;
boost::mutex::scoped_lock l(m);
MyWorkerObject w;
const boost::shared_ptr<MyScopedThing>& mst = w.getScopedThing();
}
[generic@sentinel ~]$ g++ test.cpp -o test -lboost_thread -Wall
test.cpp: In function ‘int main()’:
test.cpp:33: warning: unused variable ‘mst’
[generic@sentinel ~]$ ./test
ab[generic@sentinel ~]$ g++ -v 2>&1 | grep version
gcc version 4.4.5 20110214 (Red Hat 4.4.5-6) (GCC)
boost::mutex::scoped_lock
is a handy RAII wrapper around locking a mutex. I use a similar technique for something else: a RAII wrapper around asking a data interface to detach from/re-attach to a serial device.
What I can't figure out, though, is why in the code below only my object mst
— whose instantiation and destruction do have side effects — causes g++
to emit an "unused variable" warning error whereas l
manages to remain silent.
Do you know? Can you tell me?
[generic@sentinel ~]$ cat test.cpp
#include <boost/shared_ptr.hpp>
#include <boost/thread/mutex.hpp>
#include <iostream>
struct MyScopedThing;
struct MyWorkerObject {
void a() { std::cout << "a"; }
void b() { std::cout << "b"; }
boost::shared_ptr<MyScopedThing> getScopedThing();
};
struct MyScopedThing {
MyScopedThing(MyWorkerObject& w) : w(w) {
w.a();
}
~MyScopedThing() {
w.b();
}
MyWorkerObject& w;
};
boost::shared_ptr<MyScopedThing> MyWorkerObject::getScopedThing() {
return boost::shared_ptr<MyScopedThing>(new MyScopedThing(*this));
}
int main() {
boost::mutex m;
boost::mutex::scoped_lock l(m);
MyWorkerObject w;
const boost::shared_ptr<MyScopedThing>& mst = w.getScopedThing();
}
[generic@sentinel ~]$ g++ test.cpp -o test -lboost_thread -Wall
test.cpp: In function ‘int main()’:
test.cpp:33: warning: unused variable ‘mst’
[generic@sentinel ~]$ ./test
ab[generic@sentinel ~]$ g++ -v 2>&1 | grep version
gcc version 4.4.5 20110214 (Red Hat 4.4.5-6) (GCC)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
请注意,自其他答案撰写以来,问题已发生变化。
g++ 在当前形式下不发出警告的原因可能是因为
mst
是一个引用,并且构造和析构引用没有副作用。确实,这里的引用延长了临时对象的生命周期,这对其构造函数和析构函数有影响,但显然 g++ 没有意识到这会产生影响。Note that the question has changed since the other answers were written.
Likely the reason g++ doesn't warn in the current form is because
mst
is a reference, and constructing and destructing a reference has no side effects. It's true that here the reference is extending the lifetime of a temporary, which has effects in its constructor and destructor, but apparently g++ doesn't realise that makes a difference.如果我没记错的话,g++ 有一个不幸的习惯,即根据优化设置不同地发出
unused variable
错误,因为检测是在优化器级别进行的。也就是说,代码以 SSA 形式进行优化,如果优化器在优化后检测到某个变量未被使用,那么它可能会发出警告(我更喜欢对此进行 Clang 分析...)。
因此,这可能是检测析构函数做什么的问题。我想知道每当析构函数的定义离线时是否采取保守的方法,我推测这相当于函数调用,并且
this
符合变量的使用。If my memory serves me right, g++ has the unfortunate habit of emitting
unused variable
errors differently depending on the optimization settings because the detection works at the optimizer level.That is, the code is optimized in SSA form, and if the optimizer detects that a variable, after optimization, is unused, then it may emit a warning (I much prefer Clang analysis for this...).
Therefore it is probably a matter of detecting what the destructor does. I wonder if it takes a conservative approach whenever the definition of the destructor is offline, I would surmise this equates a function call then and that the
this
qualify as a use of the variable.我怀疑原因是你们的班级有一个琐碎的事情
析构函数,并且 g++ 仅在以下情况下警告未使用的变量:
析构函数是微不足道的。调用一个不平凡的析构函数是
“使用”。
I suspect the reason is that your class has a trivial
destructor, and that g++ only warns about unused variables if
the destructor is trivial. Invoking a non-trivial destructor is a
"use".