Skip to content

Commit

Permalink
Merge pull request #216 from N-Dekker/InitializingVariablesOfFixedSiz…
Browse files Browse the repository at this point in the history
…eArrayTypes

ENH: Add section "Initializing variables of fixed size array types"
  • Loading branch information
thewtex authored Dec 17, 2024
2 parents 1d04a60 + f10846c commit 6371836
Showing 1 changed file with 51 additions and 0 deletions.
51 changes: 51 additions & 0 deletions SoftwareGuide/Latex/Appendices/CodingStyleGuide.tex
Original file line number Diff line number Diff line change
Expand Up @@ -1580,6 +1580,9 @@ \section{The auto Keyword}
\section{Initialization and Assignment}
\label{sec:IniitalizationAndAssignment}
\subsection{Initialization of member variables}
\label{subsec:InitializationOfMemberVariables}
All member variables must be initialized when they are declared. For such
purpose, brace initializers, e.g.
Expand Down Expand Up @@ -1654,6 +1657,54 @@ \section{Initialization and Assignment}
example \code{FixedArray::m\_InternalArray} and \code{Index::m\_InternalArray}
do not have a default member initializer.
\subsection{Initializing variables of fixed size array types}
\label{subsec:InitializingVariablesOfFixedSizeArrayTypes}
ITK has various fixed size array types, including template instantiations of
\code{Index}, \code{Size}, \code{FixedArray}, \code{Point}, and \code{Vector}.
A variable of such a fixed size array type can be zero-initialized by an empty
initializer list, `{}`. This is usually the preferred way to initialize the
variable, when it should initially be filled with zeroes. For example:
\small
\begin{minted}[baselinestretch=1,fontsize=\footnotesize,linenos=false,bgcolor=ltgray]{cpp}
// Declare an index at position (0, 0).
Index<2> index{};
// Declare a point whose coordinates are all 0.0 (the origin).
PointType origin{};
\end{minted}
\normalsize
\code{Index} and \code{Size} both have a static `Filled(fillValue)` member
function, to allow creating a variable that is filled with an arbitrary value.
For these types, this is usually the preferred way to initialize the variable,
when it should initially be filled with a value that may be non-zero. For
example:
\small
\begin{minted}[baselinestretch=1,fontsize=\footnotesize,linenos=false,bgcolor=ltgray]{cpp}
// Declare the index {1, 1, 1}.
auto index = Index<3>::Filled(1);
// Declare the size of an 256 x 256 image.
auto imageSize = Size<2>::Filled(256);
\end{minted}
\normalsize
For other fixed size array types, the function `itk::MakeFilled<T>(fillValue)`
is preferable, when the array should initially be filled with a value that may
be non-zero. For example:
\small
\begin{minted}[baselinestretch=1,fontsize=\footnotesize,linenos=false,bgcolor=ltgray]{cpp}
// Declare a spacing filled with the value 0.5 (for each direction).
// SpacingType is typically defined as Vector<double, Dimension>.
auto spacing = MakeFilled<SpacingType>(0.5);
\end{minted}
\normalsize
\section{Accessing Members}
\label{sec:Accessing Members}
Expand Down

0 comments on commit 6371836

Please sign in to comment.