Topic: Forbid C-style Casts with References


Author: Jonathan Biggar <jon@floorboard.com>
Date: Thu, 11 May 2006 17:08:44 CST
Raw View
Tom   s wrote:
> First of all, there's no references in C.
>
> While C-style casts are deprecated in C++, they may still be used.
>
> However, why was their functionality *extended* to allow casting to a
> reference?

Because references were added to the language before the new-style casts
were, and so the extension was necessary.  At that point, code was
written to use C-style casts on references, so making it illegal breaks
working code.

--
Jon Biggar
Floorboard Software
jon@floorboard.com
jon@biggar.org

---
[ 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://www.comeaucomputing.com/csc/faq.html                      ]





Author: spam@spamguard.com ("Gene Bushuyev")
Date: Fri, 12 May 2006 17:26:55 GMT
Raw View
"Tom=E1s" <NULL@NULL.NULL> wrote in message=20
news:sTH8g.9041$j7.305354@news.indigo.ie...
[...]
> I propose that C-style Cast with references should be banned (i.e. they
> should not compile).
>


Treating references differently from the other types would have adverse e=
ffect=20
on templates. Since C style cast is not equivalent in general case to any=
 of c++=20
casts, it makes sense to keep it for all types instead of writing special=
ization=20
for references every time it is used. E.g.,

template<class To, class From> To c_cast(From from) { return (To)from; }

int main()
{
    int i =3D 0;
    long& l =3D c_cast<long&>(i);
}
--=20
Gene Bushuyev (www.gbresearch.com)
----------------------------------------------------------------
There is no greatness where there is no simplicity, goodness and truth. ~=
 Leo=20
Tolstoy=20

---
[ 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://www.comeaucomputing.com/csc/faq.html                      ]





Author: "David R Tribble" <david@tribble.com>
Date: 13 May 2006 15:50:06 GMT
Raw View
Tom   s wrote:
>> First of all, there's no references in C.
>> While C-style casts are deprecated in C++, they may still be used.
>>
>> However, why was their functionality *extended* to allow casting to a
>> reference?
>

Jonathan Biggar wrote:
> Because references were added to the language before the new-style casts
> were, and so the extension was necessary.  At that point, code was
> written to use C-style casts on references, so making it illegal breaks
> working code.

You compiler vendor is free to issue a warning in these cases, though.


---
[ 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://www.comeaucomputing.com/csc/faq.html                      ]





Author: ron@spamcop.net (Ron Natalie)
Date: Sun, 14 May 2006 03:46:33 GMT
Raw View
Tom=E1s wrote:
> First of all, there's no references in C.
>=20
> While C-style casts are deprecated in C++, they may still be used.

They aren't officially called C-style casts.  They don't behave like
casts do in C really.  They aren't deprecated either.  There is even
one operating mode of the C-style cast for which there is no C++
equivelent.

>=20
> However, why was their functionality *extended* to allow casting to a=20
> reference?

Because it was needed, the other casts case later.

>=20

---
[ 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://www.comeaucomputing.com/csc/faq.html                      ]





Author: "Tom s" <NULL@NULL.NULL>
Date: 11 May 2006 15:50:05 GMT
Raw View
First of all, there's no references in C.

While C-style casts are deprecated in C++, they may still be used.

However, why was their functionality *extended* to allow casting to a
reference?

Here's some sample code which compiles, but which I rather wish would not
compile:


void ModifyConstantObject( const int& a )
{
   ++(int&)a;

   /* This should have to be:

   ++const_cast<int&>(a);

   */
}

int main()
{
    int const a = 7;

    ModifyConstantObject(a);
}


I propose that C-style Cast with references should be banned (i.e. they
should not compile).

-Tom   s

---
[ 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://www.comeaucomputing.com/csc/faq.html                      ]