Complex issue around const valarray indexing

Fixes https://github.com/Itseez/opencv_contrib/issues/332

Relevant material:
http://stackoverflow.com/questions/6066273/pointer-into-vector-but-not-into-a-valarray
http://objectmix.com/c/40001-address-first-member-valarray.html
http://www.open-std.org/jtc1/sc22/wg21/docs/lwg-defects.html#389

I have to admit my naivety here, this issue is was complex for me
to diagnose. It appears that in the MSVC 2008/2010 implementation
of std::valarray, when indexing and assigning to a const variable,
that the const version of the member function
valarray<T>::operator[](size_t) returns a value instead of a
const-reference. Therefore, taking the address of this is a
compiler error as you can't take the address of an lvalue. By
performing a const_cast on the array we change which function
is called and thus we actually do recieve a const-reference.
This should not effect other compilers.
pull/338/head
Patrick Snape 9 years ago
parent 8682d7d054
commit c8986b13fc
  1. 2
      modules/bioinspired/src/transientareassegmentationmodule.cpp

@ -454,7 +454,7 @@ void TransientAreasSegmentationModuleImpl::_run(const std::valarray<float> &inpu
// first square the input in order to increase the signal to noise ratio
// get motion local energy
_squaringSpatiotemporalLPfilter(&inputToSegment[channelIndex*getNBpixels()], &_localMotion[0]);
_squaringSpatiotemporalLPfilter(&const_cast<std::valarray<float>&>(inputToSegment)[channelIndex*getNBpixels()], &_localMotion[0]);
// second low pass filter: access to the neighborhood motion energy
_spatiotemporalLPfilter(&_localMotion[0], &_neighborhoodMotion[0], 1);

Loading…
Cancel
Save