# Should we replace SafeDownCast with dynamic\_cast

**URL:** https://discourse.vtk.org/t/should-we-replace-safedowncast-with-dynamic-cast/669
**Category:** Development
**Created:** [April 2, 2019, 8:43pm UTC](https://discourse.vtk.org/t/should-we-replace-safedowncast-with-dynamic-cast/669 "2019-04-02T20:43:11Z")
**Posts on this page:** 10
**Page:** 1

<div class="post-metadata">

### Author: ![lorensen](https://discourse.vtk.org/user_avatar/discourse.vtk.org/lorensen/32/20_2.png) [@lorensen](https://discourse.vtk.org/u/lorensen)
#### Post date: [April 2, 2019, 8:43pm UTC](https://discourse.vtk.org/t/should-we-replace-safedowncast-with-dynamic-cast/669/1 "2019-04-02T20:43:11Z")

</div>

Folks,

_SafeDownCast_ was added to vtk way back when _dynamic\_cast_ was not available everywhere. Should we replace _SafeDownCast_ in vtk with _dynamic\_cast_? It is more natural for modern C++ and is well-documented. It may be more efficient although I doubt the current implementation is not a significant resource consumer.

See:

```auto
vtkKochanekSpline::SafeDownCast(this->ParametricSpline->GetXSpline())->SetDefaultTension(value);

```

versus

```auto
dynamic_cast<vtkKochanekSpline *>(this->ParametricSpline->GetXSpline())->SetDefaultTension(value);

```

It is used in over 1500 vtk files. but the changes could be automated.

Bill

---

<div class="post-metadata">

### Author: ![lorensen](https://discourse.vtk.org/user_avatar/discourse.vtk.org/lorensen/32/20_2.png) [@lorensen](https://discourse.vtk.org/u/lorensen)
#### Post date: [April 3, 2019, 6:16pm UTC](https://discourse.vtk.org/t/should-we-replace-safedowncast-with-dynamic-cast/669/2 "2019-04-03T18:16:32Z")

</div>

I did an interesting experiment. I pushed an MR that replaced the implementation of SafeDownCast to use dynamic\_cast. The first problem was using vtkAbstractHyperTreeGridMapper in vtkCellPicker. The hyper class has no subclasses in VTK. Not sure why.

I’m also getting some failed tests. I’ll continue to investigate…

---

<div class="post-metadata">

### Author: ![ken-martin](https://discourse.vtk.org/user_avatar/discourse.vtk.org/ken-martin/32/884_2.png) [@ken-martin](https://discourse.vtk.org/u/ken-martin)
#### Post date: [April 3, 2019, 7:38pm UTC](https://discourse.vtk.org/t/should-we-replace-safedowncast-with-dynamic-cast/669/3 "2019-04-03T19:38:53Z")

</div>

I like this idea Bill, seems like if we can do it, we should.

---

<div class="post-metadata">

### Author: ![toddy](https://discourse.vtk.org/letter_avatar_proxy/v4/letter/t/edb3f5/32.png) [@toddy](https://discourse.vtk.org/u/toddy)
#### Post date: [April 4, 2019, 1:50am UTC](https://discourse.vtk.org/t/should-we-replace-safedowncast-with-dynamic-cast/669/4 "2019-04-04T01:50:06Z")

</div>

`SafeDownCast` is redundant in wrapper code, so no issues there.

---

<div class="post-metadata">

### Author: ![finetjul](https://discourse.vtk.org/user_avatar/discourse.vtk.org/finetjul/32/154_2.png) [@finetjul](https://discourse.vtk.org/u/finetjul)
#### Post date: [April 9, 2019, 4:28pm UTC](https://discourse.vtk.org/t/should-we-replace-safedowncast-with-dynamic-cast/669/5 "2019-04-09T16:28:57Z")

</div>

> [@lorensen](#):
>
> It may be more efficient although I doubt the current implementation is not a significant resource consumer

I was under the impression that there are different options to improve performances of dynamic\_cast. (e.g. [http://www.stroustrup.com/fast\_dynamic\_casting.pdf](http://www.stroustrup.com/fast_dynamic_casting.pdf)). By keeping SafeDownCast, we let ourselves the option to speedup downcasting some day…

But maybe things have changed with the newests C++, I am not really up to speed here.

---

<div class="post-metadata">

### Author: ![lorensen](https://discourse.vtk.org/user_avatar/discourse.vtk.org/lorensen/32/20_2.png) [@lorensen](https://discourse.vtk.org/u/lorensen)
#### Post date: [April 16, 2019, 3:25am UTC](https://discourse.vtk.org/t/should-we-replace-safedowncast-with-dynamic-cast/669/6 "2019-04-16T03:25:19Z")

</div>

@jjomier I pushed an MR that replaced SafeDownCast with dynamic\_cast. As you suspected, there are performance issues. We are better off keeping DafeDownCast. I’m afraid I wasted some of my not-so-costly time. I am closing the MR.

See these results: [https://tinodidriksen.com/2010/04/cpp-dynamic-cast-performance/](https://tinodidriksen.com/2010/04/cpp-dynamic-cast-performance/)

Bill

---

<div class="post-metadata">

### Author: ![amaclean](https://discourse.vtk.org/user_avatar/discourse.vtk.org/amaclean/32/224_2.png) [@amaclean](https://discourse.vtk.org/u/amaclean)
#### Post date: [April 16, 2019, 5:53am UTC](https://discourse.vtk.org/t/should-we-replace-safedowncast-with-dynamic-cast/669/7 "2019-04-16T05:53:02Z")

</div>

Thanks for experimenting @lorensen we now know there are performance issues!

---

<div class="post-metadata">

### Author: ![lorensen](https://discourse.vtk.org/user_avatar/discourse.vtk.org/lorensen/32/20_2.png) [@lorensen](https://discourse.vtk.org/u/lorensen)
#### Post date: [April 18, 2019, 3:56pm UTC](https://discourse.vtk.org/t/should-we-replace-safedowncast-with-dynamic-cast/669/8 "2019-04-18T15:56:10Z")

</div>

Here’s a link to a dashboard that illustrates performance and failure related to my [dynamic\_cast experiment.](https://open.cdash.org/index.php?compare1=61&filtercount=1&field1=revision&project=VTK&showfilters=0&limit=100&value1=83dc26ad1b689795675f574272a46c755d843e06&showfeed=0)

---

<div class="post-metadata">

### Author: ![toddy](https://discourse.vtk.org/letter_avatar_proxy/v4/letter/t/edb3f5/32.png) [@toddy](https://discourse.vtk.org/u/toddy)
#### Post date: [April 19, 2019, 12:41am UTC](https://discourse.vtk.org/t/should-we-replace-safedowncast-with-dynamic-cast/669/9 "2019-04-19T00:41:45Z")

</div>

Did you identify the cause of failure with the dynamic cast on some test machines? It seems odd.

---

<div class="post-metadata">

### Author: ![lorensen](https://discourse.vtk.org/user_avatar/discourse.vtk.org/lorensen/32/20_2.png) [@lorensen](https://discourse.vtk.org/u/lorensen)
#### Post date: [April 19, 2019, 1:44am UTC](https://discourse.vtk.org/t/should-we-replace-safedowncast-with-dynamic-cast/669/10 "2019-04-19T01:44:59Z")

</div>

There were lots of discussion about this at the hackathon. Way above my level of c++ knowledge.
