Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.c++ > #9326 > unrolled thread
| Started by | "A" <a@a.a> |
|---|---|
| First post | 2011-08-25 23:58 +0200 |
| Last post | 2011-08-26 07:30 -0700 |
| Articles | 20 on this page of 51 — 20 participants |
Back to article view | Back to comp.lang.c++
which of these 3 casts would you prefer? "A" <a@a.a> - 2011-08-25 23:58 +0200
Re: which of these 3 casts would you prefer? Andrey Tarasevich <andreytarasevich@hotmail.com> - 2011-08-25 16:19 -0700
Re: which of these 3 casts would you prefer? Robert Wessel <robertwessel2@yahoo.com> - 2011-08-25 18:59 -0500
Re: which of these 3 casts would you prefer? "Alf P. Steinbach" <alf.p.steinbach:usenet@gmail.com> - 2011-08-26 08:02 +0200
Re: which of these 3 casts would you prefer? "Duga Chernobyl" <duga_c3@gmx.com> - 2011-08-26 10:05 +0200
Re: which of these 3 casts would you prefer? "A" <a@a.a> - 2011-08-26 14:40 +0200
Re: which of these 3 casts would you prefer? Leigh Johnston <leigh@i42.co.uk> - 2011-08-26 13:59 +0100
Re: which of these 3 casts would you prefer? "Duga Chernobyl" <duga_c3@gmx.com> - 2011-08-26 15:19 +0200
Re: which of these 3 casts would you prefer? Jorgen Grahn <grahn+nntp@snipabacken.se> - 2011-08-26 21:27 +0000
Re: which of these 3 casts would you prefer? "Alf P. Steinbach" <alf.p.steinbach:usenet@gmail.com> - 2011-08-27 01:28 +0200
Re: which of these 3 casts would you prefer? "Duga Chernobyl" <duga_c3@gmx.com> - 2011-08-27 10:23 +0200
Re: which of these 3 casts would you prefer? Jorgen Grahn <grahn+nntp@snipabacken.se> - 2011-08-27 12:10 +0000
Re: which of these 3 casts would you prefer? RaZiel <raziel_206_@hotmail.com> - 2011-08-27 21:55 +0200
Re: which of these 3 casts would you prefer? Miles Bader <miles@gnu.org> - 2011-09-01 08:03 +0900
Re: which of these 3 casts would you prefer? BGB <cr88192@hotmail.com> - 2011-08-31 23:11 -0700
Re: which of these 3 casts would you prefer? Jorgen Grahn <grahn+nntp@snipabacken.se> - 2011-09-01 19:34 +0000
Re: which of these 3 casts would you prefer? Joshua Maurice <joshuamaurice@gmail.com> - 2011-09-01 15:37 -0700
Re: which of these 3 casts would you prefer? gwowen <gwowen@gmail.com> - 2011-09-02 00:30 -0700
Re: which of these 3 casts would you prefer? Miles Bader <miles@gnu.org> - 2011-09-03 18:26 +0900
Re: which of these 3 casts would you prefer? Edek <edek.pienkowski@gmail.com> - 2011-08-26 15:29 +0200
Re: which of these 3 casts would you prefer? BGB <cr88192@hotmail.com> - 2011-08-27 09:35 -0700
Re: which of these 3 casts would you prefer? Juha Nieminen <nospam@thanks.invalid> - 2011-08-27 18:38 +0000
Re: which of these 3 casts would you prefer? Noah Roberts <roberts.noah@gmail.com> - 2011-08-27 12:32 -0700
Re: which of these 3 casts would you prefer? BGB <cr88192@hotmail.com> - 2011-08-27 14:51 -0700
Re: which of these 3 casts would you prefer? Jorgen Grahn <grahn+nntp@snipabacken.se> - 2011-08-29 14:10 +0000
Re: which of these 3 casts would you prefer? BGB <cr88192@hotmail.com> - 2011-08-30 07:23 -0700
Re: which of these 3 casts would you prefer? Jorgen Grahn <grahn+nntp@snipabacken.se> - 2011-08-29 11:32 +0000
Re: which of these 3 casts would you prefer? BGB <cr88192@hotmail.com> - 2011-08-27 14:37 -0700
Re: which of these 3 casts would you prefer? Ian Collins <ian-news@hotmail.com> - 2011-08-28 10:06 +1200
Re: which of these 3 casts would you prefer? BGB <cr88192@hotmail.com> - 2011-08-27 15:27 -0700
Re: which of these 3 casts would you prefer? Ian Collins <ian-news@hotmail.com> - 2011-08-28 10:47 +1200
Re: which of these 3 casts would you prefer? Noah Roberts <roberts.noah@gmail.com> - 2011-08-27 15:42 -0700
Re: which of these 3 casts would you prefer? Nobody <nobody@nowhere.com> - 2011-08-28 04:15 +0100
Re: which of these 3 casts would you prefer? Noah Roberts <roberts.noah@gmail.com> - 2011-08-27 15:52 -0700
Re: which of these 3 casts would you prefer? BGB <cr88192@hotmail.com> - 2011-08-28 01:54 -0700
Re: which of these 3 casts would you prefer? Keith H Duggar <duggar@alum.mit.edu> - 2011-09-04 19:16 -0700
Re: which of these 3 casts would you prefer? Juha Nieminen <nospam@thanks.invalid> - 2011-08-29 18:16 +0000
Re: which of these 3 casts would you prefer? Noah Roberts <roberts.noah@gmail.com> - 2011-08-29 16:35 -0700
Re: which of these 3 casts would you prefer? Nobody <nobody@nowhere.com> - 2011-08-30 04:13 +0100
Re: which of these 3 casts would you prefer? Juha Nieminen <nospam@thanks.invalid> - 2011-08-30 06:03 +0000
Re: which of these 3 casts would you prefer? BGB <cr88192@hotmail.com> - 2011-08-30 07:12 -0700
Re: which of these 3 casts would you prefer? Juha Nieminen <nospam@thanks.invalid> - 2011-08-31 06:16 +0000
Re: which of these 3 casts would you prefer? BGB <cr88192@hotmail.com> - 2011-09-01 03:05 -0700
Re: which of these 3 casts would you prefer? Juha Nieminen <nospam@thanks.invalid> - 2011-09-01 07:00 +0000
Re: which of these 3 casts would you prefer? BGB <cr88192@hotmail.com> - 2011-08-31 13:21 -0700
Re: which of these 3 casts would you prefer? Jorgen Grahn <grahn+nntp@snipabacken.se> - 2011-08-28 19:30 +0000
Re: which of these 3 casts would you prefer? Noah Roberts <roberts.noah@gmail.com> - 2011-08-27 12:19 -0700
Re: which of these 3 casts would you prefer? Dombo <dombo@disposable.invalid> - 2011-08-28 12:08 +0200
Re: which of these 3 casts would you prefer? Jorgen Grahn <grahn+nntp@snipabacken.se> - 2011-08-28 19:44 +0000
Re: which of these 3 casts would you prefer? Noah Roberts <roberts.noah@gmail.com> - 2011-08-28 13:41 -0700
Re: which of these 3 casts would you prefer? Anand Hariharan <mailto.anand.hariharan@gmail.com> - 2011-08-26 07:30 -0700
Page 1 of 3 [1] 2 3 Next page →
| From | "A" <a@a.a> |
|---|---|
| Date | 2011-08-25 23:58 +0200 |
| Subject | which of these 3 casts would you prefer? |
| Message-ID | <j36gjq$6ee$1@gregory.bnet.hr> |
int Callback(void *pThis)
{
TMyClass *pMC = (TMyClass*)pThis; // C style
TMyClass *pMC = static_cast<TMyClass*>(pThis); // C++ style but the
// same as above
TMyClass *pMC = reinterpret_cast<TMyClass*>(pThis); // Doesn't check if
// input type was
// pointer, possibly
// dangerous
}
these 3 do essentially the same. is there any preferred one for void* cast
like this and why? any difference in speed?
[toc] | [next] | [standalone]
| From | Andrey Tarasevich <andreytarasevich@hotmail.com> |
|---|---|
| Date | 2011-08-25 16:19 -0700 |
| Message-ID | <j36l85$bce$1@dont-email.me> |
| In reply to | #9326 |
On 8/25/2011 2:58 PM, A wrote:
> int Callback(void *pThis)
> {
> TMyClass *pMC = (TMyClass*)pThis; // C style
>
> TMyClass *pMC = static_cast<TMyClass*>(pThis); // C++ style but the
> // same as above
>
> TMyClass *pMC = reinterpret_cast<TMyClass*>(pThis); // Doesn't check if
> // input type was
> // pointer, possibly
> // dangerous
> }
>
> these 3 do essentially the same. is there any preferred one for void* cast
> like this and why? any difference in speed?
Casts from `void *` are handled in C++ by `static_cast`. So, that would
be the one to use in this case. There's no reason to involve a less
restrictive cast (`reinterpret_cast`) when a more restrictive does the
job. That's basically the rule I'd follow: in any situation prefer to
use the most restrictive cast that does the job.
--
Best regards,
Andrey Tarasevich
[toc] | [prev] | [next] | [standalone]
| From | Robert Wessel <robertwessel2@yahoo.com> |
|---|---|
| Date | 2011-08-25 18:59 -0500 |
| Message-ID | <udnd57lrcdopvigd5jir7dvqok8gi56ikt@4ax.com> |
| In reply to | #9326 |
On Thu, 25 Aug 2011 23:58:56 +0200, "A" <a@a.a> wrote:
>int Callback(void *pThis)
>{
>TMyClass *pMC = (TMyClass*)pThis; // C style
>
>TMyClass *pMC = static_cast<TMyClass*>(pThis); // C++ style but the
> // same as above
>
>TMyClass *pMC = reinterpret_cast<TMyClass*>(pThis); // Doesn't check if
> // input type was
> // pointer, possibly
> // dangerous
>}
>
>these 3 do essentially the same. is there any preferred one for void* cast
>like this and why? any difference in speed?
Obviously the first rule is to avoid casts where possible. I avoid
the C-style casts, simply because they don't stand out well enough,
plus they can turn into anything from a const to a reinterpret cast.
And then I try to use the "weakest" cast that does the job - even if
the "stronger" cast does the exact same thing, it has a greater
possibility of hiding an error, in either current or future code. IOW,
const_cast is preferable to static_cast, is preferable to
reinterpert_cast (which should get pretty rare usage). dynamic_cast
is a bit special, since it's not actually unsafe (although you do have
to handle the exception if it fails), and is not available in a
significant number of projects (the extra overhead dynamic_cast and
RTTI requires can usually be disabled with a compiler option).
[toc] | [prev] | [next] | [standalone]
| From | "Alf P. Steinbach" <alf.p.steinbach:usenet@gmail.com> |
|---|---|
| Date | 2011-08-26 08:02 +0200 |
| Message-ID | <j37ct4$shr$1@dont-email.me> |
| In reply to | #9326 |
On 25.08.2011 23:58, A wrote:
> int Callback(void *pThis)
> {
> TMyClass *pMC = (TMyClass*)pThis; // C style
>
> TMyClass *pMC = static_cast<TMyClass*>(pThis); // C++ style but the
> // same as above
>
> TMyClass *pMC = reinterpret_cast<TMyClass*>(pThis); // Doesn't check if
> // input type was
> // pointer, possibly
> // dangerous
> }
>
> these 3 do essentially the same. is there any preferred one for void* cast
> like this and why? any difference in speed?
Aim for clarity.
Cheers & hth.,
[toc] | [prev] | [next] | [standalone]
| From | "Duga Chernobyl" <duga_c3@gmx.com> |
|---|---|
| Date | 2011-08-26 10:05 +0200 |
| Message-ID | <op.v0s9312a42vrml@debian> |
| In reply to | #9326 |
Dnia 25-08-2011 o 23:58:56 A <a@a.a> napisał(a):
> int Callback(void *pThis)
> {
> TMyClass *pMC = (TMyClass*)pThis; // C style
>
> TMyClass *pMC = static_cast<TMyClass*>(pThis); // C++ style but the
> // same as above
>
> TMyClass *pMC = reinterpret_cast<TMyClass*>(pThis); // Doesn't check if
> // input type was
> // pointer, possibly
> // dangerous
> }
>
> these 3 do essentially the same. is there any preferred one for void*
> cast
> like this and why? any difference in speed?
>
>
>
>
IMHO there is no place for preferences.
The first is used in c and should be used only in C.
The second is from C++ and should be used mostly (compiler check if
conversion is usable)
The third is a equivalent of the c solution. Compiler checks nothing.
In c++ should be used only c++ conversion, because mostly compiler (not in
reinterpret conv.) does some checks of common sense of this conversion.
And c++ conversion are long and easy to find. The creator of c++ some gave
arguments: the place of most errors is conversion ;-)
dynamic_cast is used for child class pointers conversion.
[toc] | [prev] | [next] | [standalone]
| From | "A" <a@a.a> |
|---|---|
| Date | 2011-08-26 14:40 +0200 |
| Message-ID | <j3848g$44o$1@gregory.bnet.hr> |
| In reply to | #9331 |
I do not understand though reluctance to use C style casts. They can cast to reinterpret_cast but they also try out the weaker cast before. It is just automatic way to do what you would do manually - first try const_cast, then static_cast and finally reinterpret_cast... yes, it can use reinterpret_cast but you would have to use it anyway in a particular case where you really need it. So what's wrong with little automation, providing that you know what you are doing with casts?
[toc] | [prev] | [next] | [standalone]
| From | Leigh Johnston <leigh@i42.co.uk> |
|---|---|
| Date | 2011-08-26 13:59 +0100 |
| Message-ID | <Y7udnQBxn_haBcrTnZ2dnUVZ8nudnZ2d@giganews.com> |
| In reply to | #9333 |
On 26/08/2011 13:40, A wrote: > I do not understand though reluctance to use C style casts. > > They can cast to reinterpret_cast but they also try out the weaker cast > before. It is just automatic way to do what you would do manually - first > try const_cast, then static_cast and finally reinterpret_cast... yes, it can > use reinterpret_cast but you would have to use it anyway in a particular > case where you really need it. > > So what's wrong with little automation, providing that you know what you are > doing with casts? Using C style casts is borderline obfuscation as you are not making it clear as to what you are doing; i.e. using an explicit C++ style cast is a form of self documenting code. There is a potential for bug creation if you let a const_cast or reinterpret_cast through by mistake when using a C style cast. Bottom line: prefer C++ style casts. /Leigh
[toc] | [prev] | [next] | [standalone]
| From | "Duga Chernobyl" <duga_c3@gmx.com> |
|---|---|
| Date | 2011-08-26 15:19 +0200 |
| Message-ID | <op.v0tonlh442vrml@debian> |
| In reply to | #9333 |
Dnia 26-08-2011 o 14:40:27 A <a@a.a> napisał(a): > I do not understand though reluctance to use C style casts. > > They can cast to reinterpret_cast but they also try out the weaker cast > before. It is just automatic way to do what you would do manually - first > try const_cast, then static_cast and finally reinterpret_cast... yes, it > can > use reinterpret_cast but you would have to use it anyway in a particular > case where you really need it. > > So what's wrong with little automation, providing that you know what you > are > doing with casts? > > Sometimes you are tired and you don't want to check, if something is good or bad. For me compiler is the best friend (in programming ;-)) and always set this settings: Try search all convertions with c++ style and all with c style. What, do you think, is easier to find all of them? -Wall -Wextra -pedantic -g -pg It is not little automation, but great help ;-) if you make a mistake f.e if (a = 1) gcc will you ask, if really means it. If you are programmer, the common sense makes you to be open for all suggestions of compiler. Many people thinks, that computer is evil, but computer makes exactly what we said (but not very often what we want ;-))
[toc] | [prev] | [next] | [standalone]
| From | Jorgen Grahn <grahn+nntp@snipabacken.se> |
|---|---|
| Date | 2011-08-26 21:27 +0000 |
| Message-ID | <slrnj5g3t6.gtj.grahn+nntp@frailea.sa.invalid> |
| In reply to | #9335 |
On Fri, 2011-08-26, Duga Chernobyl wrote: > Dnia 26-08-2011 o 14:40:27 A <a@a.a> napisa??(a): > >> I do not understand though reluctance to use C style casts. >> >> They can cast to reinterpret_cast but they also try out the weaker cast >> before. It is just automatic way to do what you would do manually - first >> try const_cast, then static_cast and finally reinterpret_cast... yes, it >> can >> use reinterpret_cast but you would have to use it anyway in a particular >> case where you really need it. >> >> So what's wrong with little automation, providing that you know what you >> are >> doing with casts? > Sometimes you are tired and you don't want to check, if something is good > or bad. For me compiler is the best friend (in programming ;-)) Yes, in C++ programming anyway. > and always > set this settings: > > Try search all convertions with c++ style and all with c style. What, do > you think, is easier to find all of them? > > -Wall -Wextra -pedantic -g -pg I'm surprised you're listing g++ warning options, but omit the topical one: "-Wold-style-cast". I use it for all my code. /Jorgen -- // Jorgen Grahn <grahn@ Oo o. . . \X/ snipabacken.se> O o .
[toc] | [prev] | [next] | [standalone]
| From | "Alf P. Steinbach" <alf.p.steinbach:usenet@gmail.com> |
|---|---|
| Date | 2011-08-27 01:28 +0200 |
| Message-ID | <j39a6o$6gk$2@dont-email.me> |
| In reply to | #9352 |
On 26.08.2011 23:27, Jorgen Grahn wrote: > > I'm surprised you're listing g++ warning options, but omit the topical > one: "-Wold-style-cast". I use it for all my code. Thanks for that item. Cheers, - Alf
[toc] | [prev] | [next] | [standalone]
| From | "Duga Chernobyl" <duga_c3@gmx.com> |
|---|---|
| Date | 2011-08-27 10:23 +0200 |
| Message-ID | <op.v0u5leqk42vrml@debian> |
| In reply to | #9352 |
Dnia 26-08-2011 o 23:27:03 Jorgen Grahn <grahn+nntp@snipabacken.se> napisał(a): > On Fri, 2011-08-26, Duga Chernobyl wrote: >> Dnia 26-08-2011 o 14:40:27 A <a@a.a> napisa??(a): >> >>> I do not understand though reluctance to use C style casts. >>> >>> They can cast to reinterpret_cast but they also try out the weaker cast >>> before. It is just automatic way to do what you would do manually - >>> first >>> try const_cast, then static_cast and finally reinterpret_cast... yes, >>> it >>> can >>> use reinterpret_cast but you would have to use it anyway in a >>> particular >>> case where you really need it. >>> >>> So what's wrong with little automation, providing that you know what >>> you >>> are >>> doing with casts? > >> Sometimes you are tired and you don't want to check, if something is >> good >> or bad. For me compiler is the best friend (in programming ;-)) > > Yes, in C++ programming anyway. > >> and always >> set this settings: >> >> Try search all convertions with c++ style and all with c style. What, do >> you think, is easier to find all of them? >> >> -Wall -Wextra -pedantic -g -pg > > I'm surprised you're listing g++ warning options, but omit the topical > one: "-Wold-style-cast". I use it for all my code. > > /Jorgen > Thanks ;-)
[toc] | [prev] | [next] | [standalone]
| From | Jorgen Grahn <grahn+nntp@snipabacken.se> |
|---|---|
| Date | 2011-08-27 12:10 +0000 |
| Message-ID | <slrnj5hnkq.gtj.grahn+nntp@frailea.sa.invalid> |
| In reply to | #9359 |
On Sat, 2011-08-27, Duga Chernobyl wrote: > Dnia 26-08-2011 o 23:27:03 Jorgen Grahn <grahn+nntp@snipabacken.se> > napisa??(a): >> I'm surprised you're listing g++ warning options, but omit the topical >> one: "-Wold-style-cast". I use it for all my code. > Thanks ;-) You're welcome :-) It took me a few years to discover that flag -- I think programmers in general read their manuals to rarely. /Jorgen -- // Jorgen Grahn <grahn@ Oo o. . . \X/ snipabacken.se> O o .
[toc] | [prev] | [next] | [standalone]
| From | RaZiel <raziel_206_@hotmail.com> |
|---|---|
| Date | 2011-08-27 21:55 +0200 |
| Message-ID | <j3bi4b$7m6$1@speranza.aioe.org> |
| In reply to | #9352 |
On 26.08.2011 23:27, Jorgen Grahn wrote: > On Fri, 2011-08-26, Duga Chernobyl wrote: >> Dnia 26-08-2011 o 14:40:27 A<a@a.a> napisa??(a): >> >>> I do not understand though reluctance to use C style casts. >>> >>> They can cast to reinterpret_cast but they also try out the weaker cast >>> before. It is just automatic way to do what you would do manually - first >>> try const_cast, then static_cast and finally reinterpret_cast... yes, it >>> can >>> use reinterpret_cast but you would have to use it anyway in a particular >>> case where you really need it. >>> >>> So what's wrong with little automation, providing that you know what you >>> are >>> doing with casts? > >> Sometimes you are tired and you don't want to check, if something is good >> or bad. For me compiler is the best friend (in programming ;-)) > > Yes, in C++ programming anyway. > >> and always >> set this settings: >> >> Try search all convertions with c++ style and all with c style. What, do >> you think, is easier to find all of them? >> >> -Wall -Wextra -pedantic -g -pg > > I'm surprised you're listing g++ warning options, but omit the topical > one: "-Wold-style-cast". I use it for all my code. For those who aren't using GCC, here is an alternative (Perl script): http://www.abisource.com/mailinglists/abiword-dev/2003/May/0198.html > > /Jorgen > - RaZ
[toc] | [prev] | [next] | [standalone]
| From | Miles Bader <miles@gnu.org> |
|---|---|
| Date | 2011-09-01 08:03 +0900 |
| Message-ID | <8762ld6v5c.fsf@catnip.gol.com> |
| In reply to | #9352 |
Jorgen Grahn <grahn+nntp@snipabacken.se> writes: > I'm surprised you're listing g++ warning options, but omit the topical > one: "-Wold-style-cast". I use it for all my code. The problem with -Wold-style-cast, in my experience, is that system/library headers are often chock-full of C-style casts, resulting in huge quantities of warnings over which one has no control... -Miles -- Rational, adj. Devoid of all delusions save those of observation, experience and reflection.
[toc] | [prev] | [next] | [standalone]
| From | BGB <cr88192@hotmail.com> |
|---|---|
| Date | 2011-08-31 23:11 -0700 |
| Message-ID | <j3ohtm$uvf$1@news.albasani.net> |
| In reply to | #9579 |
On 8/31/2011 4:03 PM, Miles Bader wrote: > Jorgen Grahn<grahn+nntp@snipabacken.se> writes: >> I'm surprised you're listing g++ warning options, but omit the topical >> one: "-Wold-style-cast". I use it for all my code. > > The problem with -Wold-style-cast, in my experience, is that > system/library headers are often chock-full of C-style casts, > resulting in huge quantities of warnings over which one has no > control... > well, as well as any code which is designed to work if compiled as either C or C++ (potentially including static-inline functions in headers, ...). any such code would likely necessarily reflect the common subset of C and C++, which would mean C-style casts.
[toc] | [prev] | [next] | [standalone]
| From | Jorgen Grahn <grahn+nntp@snipabacken.se> |
|---|---|
| Date | 2011-09-01 19:34 +0000 |
| Message-ID | <slrnj5vnhq.gtj.grahn+nntp@frailea.sa.invalid> |
| In reply to | #9579 |
On Wed, 2011-08-31, Miles Bader wrote: > Jorgen Grahn <grahn+nntp@snipabacken.se> writes: >> I'm surprised you're listing g++ warning options, but omit the topical >> one: "-Wold-style-cast". I use it for all my code. > > The problem with -Wold-style-cast, in my experience, is that > system/library headers are often chock-full of C-style casts, > resulting in huge quantities of warnings over which one has no > control... Haven't seen that happen yet. Either the warning is suppressed when generated by a system header, or the ones I use (on Linux) happen to be clean. If I end up in that situation, of course I'll have to disable the warnings for the affected translation units. /Jorgen -- // Jorgen Grahn <grahn@ Oo o. . . \X/ snipabacken.se> O o .
[toc] | [prev] | [next] | [standalone]
| From | Joshua Maurice <joshuamaurice@gmail.com> |
|---|---|
| Date | 2011-09-01 15:37 -0700 |
| Message-ID | <50c94a43-f3d3-4dd4-b22b-b4a95c463c54@w22g2000prj.googlegroups.com> |
| In reply to | #9619 |
On Sep 1, 12:34 pm, Jorgen Grahn <grahn+n...@snipabacken.se> wrote: > On Wed, 2011-08-31, Miles Bader wrote: > > Jorgen Grahn <grahn+n...@snipabacken.se> writes: > >> I'm surprised you're listing g++ warning options, but omit the topical > >> one: "-Wold-style-cast". I use it for all my code. > > > The problem with -Wold-style-cast, in my experience, is that > > system/library headers are often chock-full of C-style casts, > > resulting in huge quantities of warnings over which one has no > > control... > > Haven't seen that happen yet. Either the warning is suppressed when > generated by a system header, or the ones I use (on Linux) happen to > be clean. > > If I end up in that situation, of course I'll have to disable the > warnings for the affected translation units. I seem to recall some gcc command line option which is on by default to disable warnings for system headers.
[toc] | [prev] | [next] | [standalone]
| From | gwowen <gwowen@gmail.com> |
|---|---|
| Date | 2011-09-02 00:30 -0700 |
| Message-ID | <06c3c3f9-7291-4e9c-a8d0-a89f4a555c34@en1g2000vbb.googlegroups.com> |
| In reply to | #9626 |
On Sep 1, 11:37 pm, Joshua Maurice <joshuamaur...@gmail.com> wrote: > I seem to recall some gcc command line option which is on by default > to disable warnings for system headers. Unsuprisingly, its -Wno-system-headers, and as you say, the default is to suppress them.
[toc] | [prev] | [next] | [standalone]
| From | Miles Bader <miles@gnu.org> |
|---|---|
| Date | 2011-09-03 18:26 +0900 |
| Message-ID | <87wrdqj7sj.fsf@catnip.gol.com> |
| In reply to | #9649 |
gwowen <gwowen@gmail.com> writes: >> I seem to recall some gcc command line option which is on by default >> to disable warnings for system headers. > > Unsuprisingly, its -Wno-system-headers, and as you say, the default is > to suppress them. Nonetheless, I still get tons of warnings from stuff in /usr/include, even with -Wno-system-headers, mostly from 3rd party libraries (typically C libraries -- which of course have no choice what type of cast to use -- but also some C++ libraries...). I'm not sure how it gcc distinguishes "system headers" though; the above suggests it's not simply a test for a "/usr/include" prefix... An instance where I get these warnings even from bona-fide system headers is where the cast is present in a macro expansion. E.g., <sys/mman.h> defines MAP_FAILED to be ((void *) -1). -Miles -- Somebody has to do something, and it's just incredibly pathetic that it has to be us. -- Jerry Garcia
[toc] | [prev] | [next] | [standalone]
| From | Edek <edek.pienkowski@gmail.com> |
|---|---|
| Date | 2011-08-26 15:29 +0200 |
| Message-ID | <j3877i$nfi$1@node2.news.atman.pl> |
| In reply to | #9333 |
On 08/26/2011 02:40 PM, A wrote: > I do not understand though reluctance to use C style casts. > > They can cast to reinterpret_cast but they also try out the weaker cast > before. It is just automatic way to do what you would do manually - first > try const_cast, then static_cast and finally reinterpret_cast... yes, it can > use reinterpret_cast but you would have to use it anyway in a particular > case where you really need it. > > So what's wrong with little automation, providing that you know what you are > doing with casts? > > C++ has many features you do not have to use. If you do not like this part of type enforcement, then don't, just mind others working on the same code. I would prefer static_cast, because - it is grepable (C-style cast is not so grepable) - it is more strict than reinterpret_cast and C-style. If I (or someone else) changes the type of pThis, it might rightly generate an error In both cases, it might save a lot of time otherwise spent on debugging. Edek
[toc] | [prev] | [next] | [standalone]
Page 1 of 3 [1] 2 3 Next page →
Back to top | Article view | comp.lang.c++
csiph-web