[vtk-developers] Alternatives to vtkTemplateMacro

Brad King brad.king at kitware.com
Wed Sep 9 15:12:33 EDT 2009

Hi Folks,

The "vtkArray" hierarchy was added to VTK last year to provide
N-dimensional arrays for InfoVis and other computation.  This
wiki page describes them:


Several filters already perform computation on vtkArrayData objects
(pipeline containers for vtkArray instances).

Some filters need to be implemented in terms of vtkTypedArray<>, or
even vtkDenseArray<> or vtkSparseArray<> to be efficient.  Since the
vtkTemplateMacro only supports switching on the scalar type with raw
pointers, it is insufficient for these filters.  Currently they use
hand-crafted solutions or just work for only one array type.  For
example, vtkArrayNorm only works with vtkTypedArray<double> input.

Tim Shead has proposed creation of an alternative dispatch mechanism
that supports more representations than just VTK_TT* raw pointers.
He also suggests using more modern C++ meta-programming techniques
(which is easier after dropping VS 6 and other troubled compilers):


Jeff Baumes asked me to review the problem and proposed approach.
Since any alternative to vtkTemplateMacro will be of general use
to all VTK filters, we've decided to bring the discussion here to
the VTK developer's list.

I've attached a sample program that shows one approach for runtime
dispatching to overloads selected at compile time.  It is different
than Tim's initial proposal.  An advantage shown is that no central
place (like vtkArray or vtkTemplateMacro) needs to know all the
possible types for which specialized filter implementations might

A drawback of all above dispatch mechanisms is that they work only
for one runtime-selected type at a time.  This is a problem when
both the input and output type of a filter affect the choice of
implementation.  The problem leads to double dispatch patterns like
that seen in vtkDeepCopySwitchOnOutput in vtkDataArray.cxx with

A better approach requires solving the "multiple dispatch", or
"multimethods" problem:


I briefly toyed with some ideas on library-only implementations
of multimethods in combination with overloading.  My conclusion
is that it is possible but may not be worth the complexity in
VTK for the few filters that need it.  However, new InfoVis and
other N-way array processing filters might enjoy the power.

-------------- next part --------------
A non-text attachment was scrubbed...
Name: apply_map_test.cxx
Type: text/x-c++src
Size: 4930 bytes
Desc: not available
URL: <http://public.kitware.com/pipermail/vtk-developers/attachments/20090909/12f34954/attachment-0001.cxx>

More information about the vtk-developers mailing list