|
Boost : |
From: Reece Dunn (msclrhd_at_[hidden])
Date: 2006-06-27 09:37:13
Oliver Kullman wrote:
> > NOTE: Instead of disabling the warnings, you can remove the warning by
> > updating the code. For example:
> >
> > void foo( int bar ){} // unreferenced parameter
> > void foo( int bar ){ bar; } // no warning!
> >
>
> this is FALSE:
No - it is one way to prevent the bug.
> void foo(int){}
This doesn't work if you have:
/** @brief something
* @param bar Oh dear, this is missing!
*/
void foo( int bar ){}
Either not specifying bar or commenting it out will mean that Doxygen will not
be able to identify the bar parameter.
> > bool foo(){ BOOL bar = TRUE; return bar; } // performance warning
> > bool foo(){ BOOL bar = TRUE; return bar != FALSE; } // no warning
>
> this is a perversion.
Agreed. I was giving *examples* of *possible ways* of removing the warnings
without using pragmas. I wasn't suggesting that this should be the way the code
is written!
> > class foo
> > {
> > bar & m_bar;
> > foo( bar & b ): m_bar( b ){}
> >
> > // disable the no default compiler assignment operator warning:
> > inline foo & operator=( const foo & );
> > };
>
> again this is a perversion: the C++ should follow the LANGUAGE, and the special
> handling of the *special member function* is important part of C++.
No. You miss the issue here. The problem is that m_bar is a *reference* and
*you can't assign a reference*!
> In the above example, first "inline" shouldn't be there, and then it's not
> about warnings, but about semantics: either we want the implicitely declared
> copy assignment operator, and then we *must not* add that line, or we don't
> want it, and then we *must* add the line --- there is no choice.
The inline *should* be there. This is like deriving from boost::noncopyable which
uses the above trick to prevent copying. In this case, I don't think it is possible to
use noncopyable as this is about the default assignment operator in foo. By adding
the line:
inline foo & operator=( const foo & );
I am telling the compiler that I am providing my own assignment operator, so
the compiler doesn't automatically generate one. This is the classic way to
implement non-copyable behaviour (as well as providing a similar copy constructor).
In fact, this highlights a potential issue in the code. The standard says that you
can't change a reference, so the assignment operator will copy values not what
is being pointed to. This can lead to a bug if you assign one foo to another. In
this case the warning has identified a potential problem and the above will identify
any places where that problem is manifest.
Also, explicitly disabling the variable not used warning will mask this bug:
class foo
{
int bar;
foo( int bar ): bar( bar ) {} // oops! This will be picked up by warnings!
};
- Reece
_________________________________________________________________
Try Live.com: where your online world comes together - with news, sports, weather, and much more.
http://www.live.com/getstarted
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk