Skip to content

Commit

Permalink
ENH: Declare RGBPixel, RGBAPixel constructors from component explicit
Browse files Browse the repository at this point in the history
Prevents implicit conversion of a component value to `RGBPixel` or
`RGBAPixel`. Aims to avoid possible flaws, like the one addressed by:

Pull request #3223
commit 490ef8c
"BUG: Fix `ColorTableTestSpecialConditionChecker` component/pixel mix-up"

Added _deleted_ `RGBPixel(nullptr_t)` and `RGBAPixel(nullptr_t)`
constructors, especially to avoid erroneous user code like
`RGBPixel pixel = 0`.
  • Loading branch information
N-Dekker authored and hjmjohnson committed Mar 1, 2022
1 parent 35648fa commit 0d352bd
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
9 changes: 9 additions & 0 deletions Modules/Core/Common/include/itkRGBAPixel.h
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,16 @@ class ITK_TEMPLATE_EXPORT RGBAPixel : public FixedArray<TComponent, 4>
RGBAPixel(const ComponentType r[4])
: BaseArray(r)
{}

#if defined(ITK_LEGACY_REMOVE)
/** Prevents copy-initialization from `nullptr`, as well as from `0` (NULL). */
RGBAPixel(std::nullptr_t) = delete;

/** Explicit constructor */
explicit RGBAPixel(const ComponentType & r) { this->Fill(r); }
#else
RGBAPixel(const ComponentType & r) { this->Fill(r); }
#endif

/** Pass-through assignment operator for the Array base class. */
RGBAPixel &
Expand Down
11 changes: 10 additions & 1 deletion Modules/Core/Common/include/itkRGBPixel.h
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,17 @@ class ITK_TEMPLATE_EXPORT RGBPixel : public FixedArray<TComponent, 3>
* \note The other five "special member functions" are defaulted implicitly, following the C++ "Rule of Zero". */
RGBPixel() { this->Fill(0); }

/** Constructor to fill Red=Blug=Green= r. */
#if defined(ITK_LEGACY_REMOVE)
/** Explicit constructor to fill Red=Blug=Green= r. */
explicit RGBPixel(const ComponentType & r) { this->Fill(r); }

/** Prevents copy-initialization from `nullptr`, as well as from `0` (NULL). */
RGBPixel(std::nullptr_t) = delete;
#else
/** Constructor to fill Red=Blug=Green= r.
* \note ITK_LEGACY_REMOVE=ON will disallow implicit conversion from a component value. */
RGBPixel(const ComponentType & r) { this->Fill(r); }
#endif

/** Pass-through constructor for the Array base class. */
template <typename TRGBPixelValueType>
Expand Down

0 comments on commit 0d352bd

Please sign in to comment.