How does scoped_lock avoid emitting an “unused var

2020-02-27 10:43发布

问题:

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)

回答1:

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.



回答2:

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.



回答3:

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".