Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Consider the following code :

void ListenerImpl::attach(boost::shared_ptr<ISubscriber> subscriber)
{
    boost::unique_lock<boost::mutex>(mtx);
    subscribers.push_back(subscriber);
}

void ListenerImpl::notify(MsgPtr msg)
{
    boost::unique_lock<boost::mutex>(mtx);

    //notify all subscribers
    BOOST_FOREACH(boost::shared_ptr<ISubscriber> subscriber, subscribers){
        subscriber->update(msg);
    }

}

(This is an implementation of an observer pattern as described in GoF.) The user intervention here was to protect the attach() and the notify() from simultaneous running, hence the boost::unique_lock. The goal was to protect the subscribers container.

But it is indeed extremely hard to notice that the locks are in fact just temporaries (take a closer look, there are no names assigned for them). So the lock on the mutex will be released immediately, when the temporary is destructed, i.e. the code is not thread safe. I would expect in situations like this a compiler warning. Something like "Unused temporary".

Even worse, cppcheck wouldn't recognize this mistake either. (cppcheck: a c/c++ code analysis tool http://sourceforge.net/apps/mediawiki/cppcheck/index.php?title=Main_Page)

Gcc issues warnings on unused variables. The temporary here is an unused variable, and definitely the result of the programmer's inattention. So, why there are no warnings in cases like this? Maybe it is too complicated to discover such situations?

like image 848
Gabor Marton Avatar asked Jun 29 '11 09:06

Gabor Marton


People also ask

How do I enable warnings in GCC?

Gcc 4.3+ now has -Q --help=warnings, you can even specify --help=warnings,C to just print out the C related warnings. Show activity on this post. From this page: Note that some warning flags are not implied by -Wall .

Which option of GCC inhibit all warning messages?

If -Wfatal-errors is also specified, then -Wfatal-errors takes precedence over this option. Inhibit all warning messages. Make all warnings into errors.

Which GCC flag is used to enable all compiler warnings?

gcc -Wall enables all compiler's warning messages. This option should always be used, in order to generate better code.

How does GCC treat warning errors?

You can use the -Werror compiler flag to turn all or some warnings into errors. Show activity on this post. You can use -fdiagnostics-show-option to see the -W option that applies to a particular warning. Unfortunately, in this case there isn't any specific option that covers that warning.


1 Answers

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.

like image 193
iammilind Avatar answered Oct 17 '22 02:10

iammilind