passing POD by value with const qualifiers. Silly or not?

Ingo Klöcker kloecker at kde.org
Fri Feb 22 20:50:50 GMT 2008


On Friday 22 February 2008, Stefan Teleman wrote:
> On Fri, Feb 22, 2008 at 3:16 PM, Ariya Hidayat <ariya at kde.org> wrote:
> > >  And if you change it (intentionally or not), what happens ?
> > > Anything ?
> >
> >  You can't. It will give a compile error.
>
> Yes, i know. And not only it's orthogonal to this thread, but it begs
> a more fundamental question: why would anyone ever try to modify a
> function argument passed by value. I think the point of the thread
> was about whether or not to allow constness mismatches in function
> arguments in the declaration and  the definition.
>
> I'm still waiting for a technical rationale for ever doing either of
> these.

This is a pretty stupid example, but nevertheless it should illustrate 
why it might be nice to put a const before a function argument:

void f( const int x )
{
  if ( x = -1 ) {
    return;
  } else {
    print( x );
  }
}

This will give a compile error. If the 'const' is omitted you might just 
get a warning or maybe not even a warning depending on the compiler and 
the compiler flags.

Another example would be a very long function were x is changed 
somewhere in the upper part, but in the lower part it is assumed that x 
still holds the value that was passed to the function. Obviously, this 
is more likely to happen if the lower part is written before the upper 
part and by another person.

In both cases the 'const' prevents the programmer from making a stupid 
mistake.


Regards,
Ingo
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 194 bytes
Desc: This is a digitally signed message part.
URL: <http://mail.kde.org/pipermail/kde-core-devel/attachments/20080222/90cda3ea/attachment.sig>


More information about the kde-core-devel mailing list