← Back to team overview

kicad-developers team mailing list archive

Re: Style question: static vs dynamic cast

 

On Sat, Apr 19, 2014 at 09:03:30AM -0500, Dick Hollenbeck wrote:
> For me it seems context specific, and I would not feel comfortable saying one strategy
> fits all contexts.  A dynamic cast has to be about 100 times slower than a C style cast,
> and therefore you would not use that unless you can afford the hit.

Well, a static cast should be by definition zero cost (except in newer
C++ where constructor and temporaries are allowed, or some minor vtbl
pointer tweaking), while a dynamic cast is done with an utility function
(in libsupc++, for gcc). So the cost maybe doesn't reach 100:1 but at
least 10:1 would be plausible.

If there is virtual inheritance static_cast should be the best thing
for a downcast *if* there is no virtual inheritance around (AFAIK kicad
doesn't have it) and the type is certain.

dynamic_cast is strictly needed for v.i. or when the type is not known.

So I'd recommend static_cast inside an if(Type() == x) block and
a dynamic otherwise.

Of course in 'proper' C++ style the whole Type() machinery wouldn't
exist anyway and everything should work with vtbls... maybe in 10 years,
on kicad :D


-- 
Lorenzo Marcantonio
Logos Srl


References