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

David Gobbi david.gobbi at gmail.com
Tue May 31 17:59:43 EDT 2011


I had some luck using the following typedefs to specialize the GetValue() return
value for bool:

template<typename T>
class vtkTypedArrayConstReference
{
public:
  typedef const T& ConstReference;
};

template<>
class vtkTypedArrayConstReference<bool>
{
public:
  typedef bool ConstReference;
};

With these, the GetValue methods can return the type
"typename vtkTypedArrayConstReference<T>::ConstReference"
and the compiler warning goes away (though the wrappers would
have to be modified to handle this return type).

This only fixes one of the problems with vector<bool>, but it might
be enough to make vtkTypedArray<bool> useful.

 - David


On Tue, May 31, 2011 at 9:44 AM, David Gobbi <david.gobbi at gmail.com> wrote:
> Adding a vtkTypeBool would cause more problems than it would solve.
> Avoid VTK_CHAR as an alternative, because the wrapper languages treat
> "char" as a character, not an int (use "signed char" or "unsigned char").
>
> If you have time, I'd say bite the bullet and write a vtkTypedArray<bool>
> specialization, still based on vector<bool>, but where return values are
> "bool" instead of "const bool&".
>
>  - David
>
> On Tue, May 31, 2011 at 9:29 AM, Shead, Timothy <tshead at sandia.gov> wrote:
>>
>> 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