 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Tomás Guest
|
Posted: Thu May 11, 2006 4:21 pm Post subject: Forbid C-style Casts with References |
|
|
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 ] |
|
| Back to top |
|
 |
Jonathan Biggar Guest
|
Posted: Thu May 11, 2006 10:21 pm Post subject: Re: Forbid C-style Casts with References |
|
|
Tomás wrote:
| Quote: | 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 (AT) floorboard (DOT) com
jon (AT) biggar (DOT) 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 ] |
|
| Back to top |
|
 |
Gene Bushuyev Guest
|
Posted: Fri May 12, 2006 6:21 pm Post subject: Re: Forbid C-style Casts with References |
|
|
"Tomás" <NULL (AT) NULL (DOT) NULL> wrote in message
news:sTH8g.9041$j7.305354 (AT) news (DOT) indigo.ie...
[...]
| Quote: | 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 effect
on templates. Since C style cast is not equivalent in general case to any of c++
casts, it makes sense to keep it for all types instead of writing specialization
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 = 0;
long& l = c_cast<long&>(i);
}
--
Gene Bushuyev (www.gbresearch.com)
----------------------------------------------------------------
There is no greatness where there is no simplicity, goodness and truth. ~ Leo
Tolstoy
---
[ 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 ] |
|
| Back to top |
|
 |
David R Tribble Guest
|
Posted: Sat May 13, 2006 4:21 pm Post subject: Re: Forbid C-style Casts with References |
|
|
Tomás wrote:
| Quote: | 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:
| Quote: | 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 ] |
|
| Back to top |
|
 |
Ron Natalie Guest
|
Posted: Sun May 14, 2006 4:21 am Post subject: Re: Forbid C-style Casts with References |
|
|
Tomás wrote:
| Quote: | First of all, there's no references in C.
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.
| Quote: |
However, why was their functionality *extended* to allow casting to a
reference?
|
Because it was needed, the other casts case later.
---
[ 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 ] |
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|