[LC++]ambiguity, temporaries, const

Davide Bolcioni 9odb6x3rfr001 at sneakemail.com
Sun Jan 6 03:36:05 UTC 2002


Roberto Diaz roberto at vivaldi.dhis.org [mltuxcpp/linux-cpp list] wrote:

> Hi!
> 
> The following code shows that under gcc 2.95
> 
> 		* Theres not ambiguity in f(P&) and f(const P&)


It is supposed to work that way; it is useful to be able to 

"overload on const" (and volatile) the same function. This means
that:

   void f(P &);
   void f(P const &);
   void f(P volatile &);
   void f(P const volatile &);

are four separate functions.


>         * Temporaries passed by reference default to const &


Not only this, attempting to pass a temporary to a function
taking P& (comment out the declaration of f(P const &) to see)
should result in a diagnostic. This is because generally this
is unexpected: the function would modify the temporary and
the result of such modifications would be lost as the
temporary is destroyed shortly thereafter.


>         * Non-temporaries default to non-const


Actually, declared objects have the "cv-qualification" (an abbreviation

for "const or volatile qualification" which appears in the
declaration. Combine this:

  P p;

   P const cp;
   P volatile vp;
   P const volatile cvp;

with the four overloads of f() and see what happens. All of this

is in the C++ standard.

Davide Bolcioni
-- 
There is no place like /home.






More information about the tuxCPProgramming mailing list