[vtk-developers] Problems with vtkSparseArray/DenseArray bool specialization

Shead, Timothy tshead at sandia.gov
Tue May 31 11:29:25 EDT 2011


On May 28, 2011, at 9:33 AM, David Gobbi wrote:

> Hi Tim,
> 
> When wrapping the array classes, I see the following warnings:
> 
> vtkSparseArray.txx: In member function ‘const T& vtkSparseArray<T>::GetValue(vtkIdType) [with T = bool]’:
> vtkSparseArray.txx:101: warning: returning reference to temporary
> vtkSparseArray.txx: In member function ‘const T& vtkSparseArray<T>::GetValue(vtkIdType, vtkIdType) [with T = bool]’:
> vtkSparseArray.txx:125: warning: returning reference to temporary
> vtkSparseArray.txx: In member function ‘const T& vtkSparseArray<T>::GetValue(vtkIdType, vtkIdType, vtkIdType) [with T = bool]’:
> vtkSparseArray.txx:152: warning: returning reference to temporary
> vtkSparseArray.txx: In member function ‘const T& vtkSparseArray<T>::GetValueN(vtkTypeUInt64) [with T = bool]’:
> vtkSparseArray.txx:186: warning: returning reference to temporary
> 
> These only occur for the "bool" specialization.  It took a bit of
> searching to find out why they occur, but the reason is the following:
> 
> The std::vector<bool> class is actually a bit array.  When you
> index it with [ ], it creates and returns a bool value, since it cannot
> return a const reference to a bit, but then GetValue() stores that
> bool value as a temporary, and subsequently returns a reference to it.
> 
> Can you take a look?

David:

Yes, this has come-up before and I'm familiar with the quirks of std::vector<bool>.  The "correct" solution to this issue requires template specializations for vtkSparseArray<bool> and vtkDenseArray<bool>.  The reason I haven't already done the work is that my original goal for vtkArray was to support all of the "officially sanctioned" VTK types, and "bool" isn't one of them.  The closest analogy would be VTK_BIT / vtkBitArray - although to a caller, vtkBitArray looks a like an array of ints.  I can imagine several paths forward:

* Treat VTK_BIT as-if it mapped to bool, and create the vtkArray specializations for bool.  Ignore the fact that this conflicts with vtkBitArray mapping to int.
* Create a new VTK_BOOL identifier, define a corresponding vtkTypeBool, and create vtkArray specializations for vtkTypeBool.
* Stop obsessing about it, and just create specializations for bool.
* Don't do anything, and tell people to use VTK_CHAR or similiar.

Any thoughts?

Cheers,
Tim

Timothy M. Shead
Sandia National Laboratories
1461, Scalable Analysis and Visualization





More information about the vtk-developers mailing list