C++20 Comparison Operators

Hello,

Recently Apple updated their Clang, which now supports more of the C++20 standard. Due to the changes in how the comparison operator work in C++20 (to support the spaceship operator as I understand it), I am now hitting the following error when compiling:

vcpkg_installed/x64-osx-asan/include/ITK-5.1/itkImageToHistogramFilter.hxx:294:18: error: use of overloaded operator '!=' is ambiguous (with operand types 'HistogramIterator' (aka 'itk::Statistics::Histogram<double, itk::Statistics::DenseFrequencyContainer2>::ConstIterator') and 'HistogramIterator')
      while (hit != end)
             ~~~ ^  ~~~
vcpkg_installed/x64-osx-asan/include/ITK-5.1/itkImageToHistogramFilter.h:144:3: note: in instantiation of member function 'itk::Statistics::ImageToHistogramFilter<itk::Image<float, 3> >::ThreadedMergeHistogram' requested here
  ImageToHistogramFilter();
  ^
vcpkg_installed/x64-osx-asan/include/ITK-5.1/itkImageToHistogramFilter.h:65:15: note: in instantiation of member function 'itk::Statistics::ImageToHistogramFilter<itk::Image<float, 3> >::ImageToHistogramFilter' requested here
  itkNewMacro(Self);
              ^
vcpkg_installed/x64-osx-asan/include/ITK-5.1/itkHistogramThresholdImageFilter.hxx:77:74: note: in instantiation of member function 'itk::Statistics::ImageToHistogramFilter<itk::Image<float, 3> >::New' requested here
  HistogramGeneratorPointer histogramGenerator = HistogramGeneratorType::New();
                                                                         ^
vcpkg_installed/x64-osx-asan/include/ITK-5.1/itkOtsuThresholdImageFilter.h:110:3: note: in instantiation of member function 'itk::HistogramThresholdImageFilter<itk::Image<float, 3>, itk::Image<int, 3>, itk::Image<int, 3> >::GenerateData' requested here
  OtsuThresholdImageFilter() { this->SetCalculator(CalculatorType::New()); }
  ^
vcpkg_installed/x64-osx-asan/include/ITK-5.1/itkOtsuThresholdImageFilter.h:68:15: note: in instantiation of member function 'itk::OtsuThresholdImageFilter<itk::Image<float, 3>, itk::Image<int, 3>, itk::Image<int, 3> >::OtsuThresholdImageFilter' requested here
  itkNewMacro(Self);
              ^
../Source/Core/Masking.cpp:41:72: note: in instantiation of member function 'itk::OtsuThresholdImageFilter<itk::Image<float, 3>, itk::Image<int, 3>, itk::Image<int, 3> >::New' requested here
    auto otsuFilter = itk::OtsuThresholdImageFilter<VolumeF, VolumeI>::New();
                                                                       ^
vcpkg_installed/x64-osx-asan/include/ITK-5.1/itkHistogram.h:405:5: note: candidate function
    operator!=(const ConstIterator & it)
    ^
vcpkg_installed/x64-osx-asan/include/ITK-5.1/itkHistogram.h:411:5: note: candidate function
    operator==(const ConstIterator & it)
    ^

I thought I would report here before opening an issue as I have a hunch this won’t be the only such problem, and fixing them does not always seem to be straightforward. I’ve also hit the same problem in Eigen. I don’t fully understand why these problems occur, let alone have a solution that is compatible with both C++11 and C++20.

Thanks!

I opened a PR to VNL to allow compiling with -std=c++20 i.e. CMAKE_CXX_STANDARD=20

Next fixes to follow:

I cannot reproduce your itkHistogram errors with gcc10.2.0 and CMAKE_CXX_STANDARD=20.

Those trigger for you compiling ITK or a third-party? If you provide a minimal test reproducing the issue, we can integrate it into ITK.

Hello Pablo,

I assume you are using ITK at head? I was compiling against 5.1 as contained within vcpkg. I will find time to point that to head and see what happens.

I am compiling with Apple Clang 12. The compiler support grid on cppreference.com does not seem to have been updated for that yet.

Hi @spinicist,
indeed, I am testing at head of master.

I can reproduce it with clang 10.0.1 and CMAKE_CXX_STANDARD=20.

I have created a new PR fixing const-correctness of operator== and operator!= in itkHistogram and few other iterators.

1 Like

I see this was merged, thanks very much!

1 Like