Why is there no gcc/g++ warning for unused temporaries?

前端 未结 3 814
感情败类
感情败类 2020-12-17 18:17

Consider the following code :

void ListenerImpl::attach(boost::shared_ptr subscriber)
{
    boost::unique_lock(mtx);
          


        
3条回答
  •  清歌不尽
    2020-12-17 19:00

    hmm.. I am not sure but can't this be protected against with normal c++ also?

    class Mutex;
    class Lock {
        Lock(Mutex *mutex);
    };
    
    int main() {
        Lock /* lock */ (&mtx);
        return 0;
    }
    

    I get this compiler warning when compiling with DJGPP:

    C:\df>gxx -c a.cpp
    a.cpp: In function 'int main()':
    a.cpp:8:30: error: 'mtx' declared as reference but not initialized
    

    It compiles fine if I uncomment "lock" and add a mutex variable.

    So if your "mtx" variable is a pointer. What happens if you change it and pass it as "&mtx" instead.

提交回复
热议问题