Topic: const references [dcl.ref] - why shall they be ill-formed?
Author: raimund6607@my-deja.com
Date: 2000/07/11 Raw View
According to ISO/IEC 14882:1998, 8.3.2 1 the following shall be ill-
formed.
typedef int& A;
void main (void)
{
int i;
const A aref = i;
}
But Borland C++ 5.5 for example compiles it without any problems.
Why does the standard forbid the above?
Sent via Deja.com http://www.deja.com/
Before you buy.
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: wmm@fastdial.net
Date: 2000/07/12 Raw View
In article <8kefjj$gf$1@nnrp1.deja.com>,
raimund6607@my-deja.com wrote:
> According to ISO/IEC 14882:1998, 8.3.2 1 the following shall be ill-
> formed.
>
> typedef int& A;
>
> void main (void)
> {
> int i;
> const A aref = i;
> }
>
> But Borland C++ 5.5 for example compiles it without any problems.
> Why does the standard forbid the above?
No, this is well-formed according to the Standard.
Cv-qualification is permissible when introduced via typedef,
just not when introduced directly in the declarator. Thus
"const A aref = i;" is okay, but "int & const aref2 = i;"
is not, even though the type specification is the same in
both cases.
(The example in 8.3.2p1 is confusing. If you don't read it
carefully, it seems to indicate that your example is
ill-formed. However, what it's really saying is that the
type of "const A" is "int &", not "const int&", and thus the
initialization with the literal value 3 is ill-formed --
as indicated by the comment in the example, "non-const
reference initialized with rvalue." In your version of the
example, the initializer is a non-const lvalue, so there's
no problem.)
As for why this is ill-formed, it was to avoid confusion;
since you can't change what a reference refers to after it's
initialized, it's already as "const" as you can make it.
Allowing a "const" on a reference might be taken as having
some meaning, when in fact it would be completely
meaningless.
--
William M. Miller, wmm@fastdial.net
Vignette Corporation (www.vignette.com)
Sent via Deja.com http://www.deja.com/
Before you buy.
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: Christopher Eltschka <celtschk@physik.tu-muenchen.de>
Date: 2000/07/12 Raw View
raimund6607@my-deja.com wrote:
>
> According to ISO/IEC 14882:1998, 8.3.2 1 the following shall be ill-
> formed.
>
> typedef int& A;
>
> void main (void)
You mis-spelled int.
> {
> int i;
> const A aref = i;
> }
>
> But Borland C++ 5.5 for example compiles it without any problems.
> Why does the standard forbid the above?
Because this would be the equivalent to
int main()
{
int i;
int& const aref = i;
}
and that's illegal code.
I guess what you really wanted is
int main()
{
int i;
int const& aref = i;
}
but applying const to a typedef'ed type always applies const
to the complete type, not to some type it is built of.
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]