vtkObject and C++ const-ness - not possible?

Hello, I just wanted to be sure on this topic:

is it correct, that VTK basically gives up on using const-ness in C++ sense?

For example, it is not meaningful to e.g. implement an algorithm, which would expost its result (OutputData) as a const vtkDataSet, because it is not possible to send const vtkDataSet as input to other altorithms or to mapper for visualisation. Is my understanding correct?

Thank you.

Hi, Dmitrii,

My two cents on this is that a great deal of VTK code is quite old and was not refactored yet to adopt modern practices such as using const. Hence, the lack of constness tend to propagate to newer API.



Yes, first commit of VTK was made in 1994 - four years before the first C++ standard was published. Concept of const was already around at that time but probably not widely used nor universally supported by compilers.

Improving const usage apparently has not been a top priority in the last 27 years and the fact that VTK could grow and remain robust and extensible without it has proven that this was not a bad decision. Introducing consts in VTK would be certainly nice, but I could list several other infrastructural improvements with much better cost/benefit ratio.

1 Like

I remember seeing a branch 5-6 years ago trying to introduce const correctness into VTK. Sadly, it was not possible without completely breaking retro-compatibility.

@lassoan, @mwestphal:
Thank you for your comments. My question was not intended to give any judjment, bjut just to ensure, that my understanding is correct.

I understand, that in the projects that big and that long running as VTK there may be design desisions, which are not appreciated by some decelopers as fortunate. But there are always reasons for this (often - historical reasons, but sometimes something else). And indeed VTK prooved to be good and useful enough to sustain this long, no matter if it is despite of or because of those design descisions.

For this particular topic in my opinion the problem is that it is not easy to build VTK into some project, which tries to keep modern const-correctness well formed. If someone has some ideas (patterns), how to do it, it would be worth sharing in the documentation or in the book.

I personally will try to get away with using “mutable” for VTK objects inside my own classes… It all is a bit messy, but let’s see, how it goes. In the worst case I will have to give up const-correctness…

Another point about introducing it into VTK. I understand, that it is huge work. And the one, how does this job must clearly understand the dependencies. E.g. it makes sense to introduce const-ness first into rendering machinery and only then into algorithms, because as I wrote in the original question it does not make sense to have Algorithm returning const result, if Mapper expects non-const input. So one would have to begin in the reverse direction: renderer, actor, mapper etc. (maybe I am missing something).

1 Like

It would already be a huge help to have data classes const-correct, e.g. vtkPolyData.
Since the algorithms basically deep copy input data anyways this should be possible and make writing algorithms much safer.

Just const cast when needed, before calling VTK.

1 Like