c++ - How does scoped_lock avoid emitting an "unused variable" warning? -


boost::mutex::scoped_lock handy raii wrapper around locking mutex. use similar technique else: raii wrapper around asking data interface detach from/re-attach serial device.

what can't figure out, though, why in code below object mst — instantiation , destruction do have side effects — causes g++ emit "unused variable" warning error whereas l manages remain silent.

do know? can 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 question has changed since other answers written.

likely reason g++ doesn't warn in current form because mst reference, , constructing , destructing reference has no side effects. it's true here reference extending lifetime of temporary, has effects in constructor , destructor, apparently g++ doesn't realise makes difference.


Comments

Popular posts from this blog

how to proxy from https to http with lighttpd -

android - Automated my builds -

python - Flask migration error -