Topic: const references (may be banned by ISO/ANSI committee)


Author: maxtal@physics.su.OZ.AU (John Max Skaller)
Date: Mon, 30 May 1994 16:41:11 GMT
Raw View
In article <2s36pb$ip3@bigfoot.wustl.edu> a.hammoud@ieee.org writes:
>Hello,
>
>I just read the C++ puzle in the C++ report by Rob Murray. In his column
>he said "Believe it or not, the ISO/ANSI committe has actually spent time
>discussing const references, and it is entirely possible that they may ban
>them".
>
>// operator [],  returns constant reference to a pointer.
>template <class T>
>inline T* const & PtrBag<T>::operator [] (int i) const {
>   return b[i];
>}
>
>So my question, is what I am doing above not portable and soon will
>become not-supported.

 Nope. you code is safe as houses. You have a refernce to
a const pointer here, sometimes called a const reference but not
the type the ANSI/ISO committee has been discussion:

 T& const var; // GAK!

--
        JOHN (MAX) SKALLER,         INTERNET:maxtal@suphys.physics.su.oz.au
 Maxtal Pty Ltd,      CSERVE:10236.1703
        6 MacKay St ASHFIELD,     Mem: SA IT/9/22,SC22/WG21
        NSW 2131, AUSTRALIA




Author: jason@cygnus.com (Jason Merrill)
Date: Tue, 31 May 1994 05:28:35 GMT
Raw View
>>>>> Olivier Galibert aka Sarayan <galibero@mines.u-nancy.fr> writes:

> Did you throw away things like that too :

> X& X::operator =(const X&)

No.  A const reference to X is written 'X& const', whereas a reference to a
const X is written 'const X&' or 'X const &'.  g++ now converts a const
reference into a non-const reference with a warning.  References to const
objects are not affected.

Jason





Author: jbuck@synopsys.com (Joe Buck)
Date: 31 May 1994 16:34:46 GMT
Raw View
Olivier Galibert aka Sarayan <galibero@mines.u-nancy.fr> writes:
>> Did you throw away things like that too :
>> X& X::operator =(const X&)

jason@cygnus.com (Jason Merrill) writes:
>No.  A const reference to X is written 'X& const', whereas a reference to a
>const X is written 'const X&' or 'X const &'.  g++ now converts a const
>reference into a non-const reference with a warning.  References to const
>objects are not affected.

But *all* references are const, so the const keyword is redundant.
References, like const objects, must be initialized at the point of
definition and can never be changed to refer to a different object
(in the case of a reference) or to hold a different value (in the
case of a const object).  So it isn't true that "g++ now converts a
const reference into a non-const reference" -- so the warning is
appropriate, but perhaps you'll want to re-word it.



--
-- Joe Buck  <jbuck@synopsys.com>
Posting from but not speaking for Synopsys, Inc.
***** Stamp out junk e-mail spamming!  If someone sends you a junk e-mail
***** ad just because you posted in comp.foo, boycott their company.




Author: abed@cec1.wustl.edu (Abed M. Hammoud)
Date: 26 May 1994 22:10:51 GMT
Raw View
Hello,

I just read the C++ puzle in the C++ report by Rob Murray. In his column
he said "Believe it or not, the ISO/ANSI committe has actually spent time
discussing const references, and it is entirely possible that they may ban
them".

If possible could some one please elaborate on this. I have built
a bag of pointers class in which I define  the following two operators:
(dont ask why, but it is very usefull for me):

inline T*& operator [] (int);
inline T* const & operator [] (int) const;

// operator [],  returns reference to a pointer.
template <class T>
inline T*& PtrBag<T>::operator [] (int i) {
   return b[i];
}

where b is of type T**

// operator [],  returns constant reference to a pointer.
template <class T>
inline T* const & PtrBag<T>::operator [] (int i) const {
   return b[i];
}

basically the second operator return a const reference to a pointer.

So my question, is what I am doing above not portable and soon will
become not-supported.

Thanks,
Abed M. Hammoud




Author: jason@cygnus.com (Jason Merrill)
Date: Mon, 30 May 1994 08:57:48 GMT
Raw View
>>>>> Abed M Hammoud <abed@cec1.wustl.edu> writes:

> inline T* const & operator [] (int) const;

> basically the second operator return a const reference to a pointer.

No, the second operator returns a reference to a const pointer.  A const
reference to a pointer would be

inline T*& const operator [] (int) const;

What do you suppose that means?  I recently modified g++ to throw away
const and volatile qualifiers applied to references.

> So my question, is what I am doing above not portable and soon will
> become not-supported.

Nope, what you are doing is fine.

Jason




Author: galibero@mines.u-nancy.fr (Olivier Galibert aka Sarayan)
Date: 30 May 1994 10:40:57 GMT
Raw View
In article <JASON.94May30015748@deneb.cygnus.com>, jason@cygnus.com (Jason
Merrill) writes:
> [...] I recently modified g++ to throw away
> const and volatile qualifiers applied to references.

Did you throw away things like that too :

X& X::operator =(const X&)
{
  ...
}

or

X::X(const X&)
{
 ...
}


Which is *very* useful ?

  Sarayan