如何避免scoped_lock的发光的"未使用的变量"警告? [英] How does scoped_lock avoid emitting an "unused variable" warning?
问题描述
的boost ::互斥:: scoped_lock的
是围绕锁定一个互斥体一个方便的RAII包装。我使用了类似的技术,别的东西:一个RAII包装器询问一个数据接口,从分离/重新连接到串行设备
我想不出什么,虽然是为什么只有我的对象 MST
&MDASH低于code;其实例化和破坏的确实有副作用的—而→
管理,以保持沉默的原因 G ++
发出一个未使用变量警告错误。
你知道吗?你能告诉我吗?
[通用@定点〜] $猫TEST.CPP
#包括LT&;升压/ shared_ptr.hpp>
#包括LT&;升压/线程/ mutex.hpp>
#包括LT&;&iostream的GT;结构MyScopedThing;
结构MyWorkerObject {
一个无效(){性病::法院LT&;< 一个; }
空穴B(){性病::法院LT&;< B; } 提高:: shared_ptr的< MyScopedThing> getScopedThing();
};结构MyScopedThing {
MyScopedThing(MyWorkerObject&安培; W):W(W){
W.A();
}
〜MyScopedThing(){
W.B();
} MyWorkerObject&安培;瓦;
};提高:: shared_ptr的< MyScopedThing> MyWorkerObject :: getScopedThing(){
返回的boost :: shared_ptr的< MyScopedThing>(新MyScopedThing(*本));
}诠释主(){
提高::互斥米;
提高::互斥:: scoped_lock的L(M); MyWorkerObject瓦;
常量的boost :: shared_ptr的< MyScopedThing>&安培; MST = w.getScopedThing();
}
[通用@定点〜] $ G ++ TEST.CPP -o测试-lboost_thread -Wall
TEST.CPP:在函数'廉政的main():
TEST.CPP:33:警告:未使用的变量'MST'[通用@定点〜] $ ./test
AB [通用@定点〜] $ G ++ -v 2 - ;&放大器; 1 | grep的版本
gcc版本4.4.5 20110214(红帽4.4.5-6)(GCC)
请注意,这个问题已经改变,因为其他答案写。
有可能的原因G ++不会在目前的形式警告是因为 MST
是一个参考,构建和销毁的引用没有副作用。这是真的,这里的参考延长临时的寿命,它在它的构造函数和析构函数的影响,但显然G ++并没有意识到有差别。
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)
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.
这篇关于如何避免scoped_lock的发光的&QUOT;未使用的变量&QUOT;警告?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!