On another note - unwanted warnings: one well known C++ warning is "unused variable XXX". Today I ran into two examples of when this warning was just, but I had to "cheat" it:
- In Qt there's a convenient idiom of dynamic objects getting their parent on creation and "registering to it". The pointer to the object is no longer needed - the parent holds it, and takes care to destroy it. This way there is much less memory trouble when using Qt. It looks like this:
ChildObj* obj = new ChildObj(ptr_parent, foo, bar);
Since obj is never used afterwards, it leads to a "unused variable" warning. It can be solved like this:(void) new ChildObj(ptr_parent, foo, bar);
The (void) is necessary because otherwise the compiler complains about an unassigned allocation. -
A trickier problem. There's an idiom of resource management that creates dummy objects whose only purpose is to release some resource on destruction. Then, using these objects as autos is useful because we're making certain that the release will happen even if an exception is thrown:
class Killer { Killer() {} ~Killer() {globalResource->release();} } ... ... { Killer k; ... ... // here we know for sure that the resource will be released }
The compiler cries "unused variable k" and here I still don't know how to solve it :-(