This is the mail archive of the
gcc-bugs@gcc.gnu.org
mailing list for the GCC project.
[Bug c++/16335] passing non-const pointer to const pointer reference causes copy
- From: "bangerth at dealii dot org" <gcc-bugzilla at gcc dot gnu dot org>
- To: gcc-bugs at gcc dot gnu dot org
- Date: 2 Jul 2004 20:54:43 -0000
- Subject: [Bug c++/16335] passing non-const pointer to const pointer reference causes copy
- References: <20040702195231.16335.cnbfxc@mizzou.edu>
- Reply-to: gcc-bugzilla at gcc dot gnu dot org
------- Additional Comments From bangerth at dealii dot org 2004-07-02 20:54 -------
gccs behavior seems correct to me: the constructor expects a
reference to const int*, whereas you provide it with an int*.
The latter is not a const-qualified version of the former, but
they are convertible to the former type, so the compiler
generates a temporary of type const int* and takes the reference
to this temporary. (Note that here you are on thin ice anyway,
since you store the address of a temporary, which invokes undefined
behavior.)
To fix your code, present the constructor with a const int*, like so
const int *p = &a;
With this, you get the desired output.
W.
--
What |Removed |Added
----------------------------------------------------------------------------
Status|UNCONFIRMED |RESOLVED
Resolution| |INVALID
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=16335