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

前端 未结 3 809
感情败类
感情败类 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:02

    Compiler doesn't issue a warning, because it's quite possible that you might be updating some static-member / global variable inside the constructor (which is valid and meaningful). e.g.:

    struct A
    {
      static int count;
      A () { count ++; }
    };
    

    Now when you simply invoke a temporary:

    A();
    

    In case if such update is not happening, compiler will not dig into the constructor of A and check if something useful is happening. It always assumes to be a valid scenario. There are many such cases can be pointed out related to temporaries.

提交回复
热议问题