Skip to content

Commit

Permalink
ENH: Make default-constructors of RGBPixel and RGBAPixel constexpr
Browse files Browse the repository at this point in the history
Tested by means of `CheckConstexprBeginAndEndOfContainer()`. Note that these
default-constructors were already `constexpr` _implicitly_, as implied by
`= default`, when `ITK_FUTURE_LEGACY_REMOVE` would be enabled.

Used the expression `Superclass(Superclass())` as a workaround for a compiler
bug from GCC 9.4.0, causing warnings like:

  In file included from itkNumericTraitsTest.cxx:42:
  itkNumericTraitsRGBPixel.h: In function 'CheckVariableLengthArrayTraits(const T&) [with T = itk::RGBPixel<char>]':
  itkNumericTraitsRGBPixel.h:118:17: warning: '<anonymous>' may be used uninitialized in this function [-Wmaybe-uninitialized]
  • Loading branch information
N-Dekker authored and hjmjohnson committed Dec 18, 2024
1 parent c9b342e commit 52f6414
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 2 deletions.
1 change: 1 addition & 0 deletions .github/workflows/itk_dict.txt
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,7 @@ Wachowiak
Wanlin
Wdeprecated
Wi
Wmaybe
Wtautological
Xu
Y'CbCr
Expand Down
8 changes: 7 additions & 1 deletion Modules/Core/Common/include/itkRGBAPixel.h
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,13 @@ class ITK_TEMPLATE_EXPORT RGBAPixel : public FixedArray<TComponent, 4>
#ifdef ITK_FUTURE_LEGACY_REMOVE
RGBAPixel() = default;
#else
RGBAPixel() { this->Fill(0); }
constexpr RGBAPixel()
: Superclass(Superclass())
{
// `: Superclass(Superclass())` is a workaround for an old compiler bug. A simple `: Superclass()` triggered
// warnings from GCC 9.4.0 saying: "warning: '<anonymous>' may be used uninitialized in this function
// [-Wmaybe-uninitialized]".
}
#endif

/** Pass-through constructor for the Array base class. */
Expand Down
8 changes: 7 additions & 1 deletion Modules/Core/Common/include/itkRGBPixel.h
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,13 @@ class ITK_TEMPLATE_EXPORT RGBPixel : public FixedArray<TComponent, 3>
#ifdef ITK_FUTURE_LEGACY_REMOVE
RGBPixel() = default;
#else
RGBPixel() { this->Fill(0); }
constexpr RGBPixel()
: Superclass(Superclass())
{
// `: Superclass(Superclass())` is a workaround for an old compiler bug. A simple `: Superclass()` triggered
// warnings from GCC 9.4.0 saying: "warning: '<anonymous>' may be used uninitialized in this function
// [-Wmaybe-uninitialized]".
}
#endif

#if defined(ITK_LEGACY_REMOVE)
Expand Down
5 changes: 5 additions & 0 deletions Modules/Core/Common/test/itkRGBAPixelGTest.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@
#include <gtest/gtest.h>


static_assert(itk::RangeGTestUtilities::CheckConstexprBeginAndEndOfContainer<itk::RGBAPixel<>>() &&
itk::RangeGTestUtilities::CheckConstexprBeginAndEndOfContainer<itk::RGBAPixel<std::uint8_t>>() &&
itk::RangeGTestUtilities::CheckConstexprBeginAndEndOfContainer<itk::RGBAPixel<float>>());


// Tests that a RGBAPixel that is "value-initialized" (by empty braces, `{}`) is zero-filled.
TEST(RGBAPixel, ValueInitializedIsZeroFilled)
{
Expand Down
5 changes: 5 additions & 0 deletions Modules/Core/Common/test/itkRGBPixelGTest.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@
#include <gtest/gtest.h>


static_assert(itk::RangeGTestUtilities::CheckConstexprBeginAndEndOfContainer<itk::RGBPixel<>>() &&
itk::RangeGTestUtilities::CheckConstexprBeginAndEndOfContainer<itk::RGBPixel<std::uint8_t>>() &&
itk::RangeGTestUtilities::CheckConstexprBeginAndEndOfContainer<itk::RGBPixel<float>>());


// Tests that a RGBPixel that is "value-initialized" (by empty braces, `{}`) is zero-filled.
TEST(RGBPixel, ValueInitializedIsZeroFilled)
{
Expand Down

0 comments on commit 52f6414

Please sign in to comment.