Skip to content

Commit f10846c

Browse files
committed
ENH: Add section "Initializing variables of fixed size array types"
Suggested by Jon Haitz Legarreta Gorroño at InsightSoftwareConsortium/ITK#5024 (comment)
1 parent 1d04a60 commit f10846c

File tree

1 file changed

+51
-0
lines changed

1 file changed

+51
-0
lines changed

SoftwareGuide/Latex/Appendices/CodingStyleGuide.tex

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1580,6 +1580,9 @@ \section{The auto Keyword}
15801580
\section{Initialization and Assignment}
15811581
\label{sec:IniitalizationAndAssignment}
15821582
1583+
\subsection{Initialization of member variables}
1584+
\label{subsec:InitializationOfMemberVariables}
1585+
15831586
All member variables must be initialized when they are declared. For such
15841587
purpose, brace initializers, e.g.
15851588
@@ -1654,6 +1657,54 @@ \section{Initialization and Assignment}
16541657
example \code{FixedArray::m\_InternalArray} and \code{Index::m\_InternalArray}
16551658
do not have a default member initializer.
16561659
1660+
\subsection{Initializing variables of fixed size array types}
1661+
\label{subsec:InitializingVariablesOfFixedSizeArrayTypes}
1662+
1663+
ITK has various fixed size array types, including template instantiations of
1664+
\code{Index}, \code{Size}, \code{FixedArray}, \code{Point}, and \code{Vector}.
1665+
1666+
A variable of such a fixed size array type can be zero-initialized by an empty
1667+
initializer list, `{}`. This is usually the preferred way to initialize the
1668+
variable, when it should initially be filled with zeroes. For example:
1669+
1670+
\small
1671+
\begin{minted}[baselinestretch=1,fontsize=\footnotesize,linenos=false,bgcolor=ltgray]{cpp}
1672+
// Declare an index at position (0, 0).
1673+
Index<2> index{};
1674+
1675+
// Declare a point whose coordinates are all 0.0 (the origin).
1676+
PointType origin{};
1677+
\end{minted}
1678+
\normalsize
1679+
1680+
\code{Index} and \code{Size} both have a static `Filled(fillValue)` member
1681+
function, to allow creating a variable that is filled with an arbitrary value.
1682+
For these types, this is usually the preferred way to initialize the variable,
1683+
when it should initially be filled with a value that may be non-zero. For
1684+
example:
1685+
1686+
\small
1687+
\begin{minted}[baselinestretch=1,fontsize=\footnotesize,linenos=false,bgcolor=ltgray]{cpp}
1688+
// Declare the index {1, 1, 1}.
1689+
auto index = Index<3>::Filled(1);
1690+
1691+
// Declare the size of an 256 x 256 image.
1692+
auto imageSize = Size<2>::Filled(256);
1693+
\end{minted}
1694+
\normalsize
1695+
1696+
For other fixed size array types, the function `itk::MakeFilled<T>(fillValue)`
1697+
is preferable, when the array should initially be filled with a value that may
1698+
be non-zero. For example:
1699+
1700+
\small
1701+
\begin{minted}[baselinestretch=1,fontsize=\footnotesize,linenos=false,bgcolor=ltgray]{cpp}
1702+
// Declare a spacing filled with the value 0.5 (for each direction).
1703+
// SpacingType is typically defined as Vector<double, Dimension>.
1704+
auto spacing = MakeFilled<SpacingType>(0.5);
1705+
\end{minted}
1706+
\normalsize
1707+
16571708
\section{Accessing Members}
16581709
\label{sec:Accessing Members}
16591710

0 commit comments

Comments
 (0)