Skip to content

Commit b74cc5f

Browse files
committed
STYLE: Prefer C++11 type alias over typedef
== http://en.cppreference.com/w/cpp/language/type_alias == Type alias is a name that refers to a previously defined type (similar to typedef). A type alias declaration introduces a name which can be used as a synonym for the type denoted by type-id. It does not introduce a new type and it cannot change the meaning of an existing type name. There is no difference between a type alias declaration and typedef declaration. This declaration may appear in block scope, class scope, or namespace scope. == https://www.quora.com/Is-using-typedef-in-C++-considered-a-bad-practice == While typedef is still available for backward compatibility, the new Type Alias syntax 'using Alias = ExistingLongName;' is more consistent with the flow of C++ than the old typedef syntax 'typedef ExistingLongName Alias;', and it also works for templates (Type alias, alias template (since C++11)), so leftover 'typedef' aliases will differ in style from any alias templates.
1 parent 0e08f74 commit b74cc5f

9 files changed

+197
-209
lines changed

examples/example.cxx

Lines changed: 26 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -21,16 +21,16 @@ int example_gradientdescent( int argc, char* argv[] )
2121
{
2222
// Typedefs
2323
const unsigned int Dimension = 2;
24-
typedef float PixelType;
25-
typedef unsigned char OutputPixelType;
26-
typedef itk::Image< PixelType, Dimension > ImageType;
27-
typedef itk::Image< OutputPixelType, Dimension > OutputImageType;
28-
typedef itk::ImageFileReader< ImageType > ReaderType;
29-
typedef itk::ImageFileWriter< OutputImageType > WriterType;
30-
typedef itk::PolyLineParametricPath< Dimension > PathType;
31-
typedef itk::SpeedFunctionToPathFilter< ImageType, PathType > PathFilterType;
32-
typedef PathFilterType::CostFunctionType::CoordRepType CoordRepType;
33-
typedef itk::PathIterator< OutputImageType, PathType > PathIteratorType;
24+
using PixelType = float;
25+
using OutputPixelType = unsigned char;
26+
using ImageType = itk::Image< PixelType, Dimension >;
27+
using OutputImageType = itk::Image< OutputPixelType, Dimension >;
28+
using ReaderType = itk::ImageFileReader< ImageType >;
29+
using WriterType = itk::ImageFileWriter< OutputImageType >;
30+
using PathType = itk::PolyLineParametricPath< Dimension >;
31+
using PathFilterType = itk::SpeedFunctionToPathFilter< ImageType, PathType >;
32+
using CoordRepType = PathFilterType::CostFunctionType::CoordRepType;
33+
using PathIteratorType = itk::PathIterator< OutputImageType, PathType >;
3434

3535
// Get filename arguments
3636
unsigned int argi = 1;
@@ -45,8 +45,7 @@ ImageType::Pointer speed = reader->GetOutput();
4545
speed->DisconnectPipeline();
4646

4747
// Create interpolator
48-
typedef itk::LinearInterpolateImageFunction<ImageType, CoordRepType>
49-
InterpolatorType;
48+
using InterpolatorType = itk::LinearInterpolateImageFunction<ImageType, CoordRepType>;
5049
InterpolatorType::Pointer interp = InterpolatorType::New();
5150

5251
// Create cost function
@@ -55,7 +54,7 @@ PathFilterType::CostFunctionType::Pointer cost =
5554
cost->SetInterpolator( interp );
5655

5756
// Create optimizer
58-
typedef itk::GradientDescentOptimizer OptimizerType;
57+
using OptimizerType = itk::GradientDescentOptimizer;
5958
OptimizerType::Pointer optimizer = OptimizerType::New();
6059
optimizer->SetNumberOfIterations( 1000 );
6160

@@ -73,7 +72,7 @@ end[0] = 100; end[1] = 10;
7372
way1[0] = 10; way1[1] = 10;
7473

7574
// Add path information
76-
typedef PathFilterType::PathInformationType PathInformationType;
75+
using PathInformationType = PathFilterType::PathInformationType;
7776
PathInformationType::Pointer info = PathInformationType::New();
7877
info->SetStartPoint( start );
7978
info->SetEndPoint( end );
@@ -126,16 +125,16 @@ int example_regularstepgradientdescent( int argc, char* argv[] )
126125
{
127126
// Typedefs
128127
const unsigned int Dimension = 2;
129-
typedef float PixelType;
130-
typedef unsigned char OutputPixelType;
131-
typedef itk::Image< PixelType, Dimension > ImageType;
132-
typedef itk::Image< OutputPixelType, Dimension > OutputImageType;
133-
typedef itk::ImageFileReader< ImageType > ReaderType;
134-
typedef itk::ImageFileWriter< OutputImageType > WriterType;
135-
typedef itk::PolyLineParametricPath< Dimension > PathType;
136-
typedef itk::SpeedFunctionToPathFilter< ImageType, PathType > PathFilterType;
137-
typedef PathFilterType::CostFunctionType::CoordRepType CoordRepType;
138-
typedef itk::PathIterator< OutputImageType, PathType > PathIteratorType;
128+
using PixelType = float;
129+
using OutputPixelType = unsigned char;
130+
using ImageType = itk::Image< PixelType, Dimension >;
131+
using OutputImageType = itk::Image< OutputPixelType, Dimension >;
132+
using ReaderType = itk::ImageFileReader< ImageType >;
133+
using WriterType = itk::ImageFileWriter< OutputImageType >;
134+
using PathType = itk::PolyLineParametricPath< Dimension >;
135+
using PathFilterType = itk::SpeedFunctionToPathFilter< ImageType, PathType >;
136+
using CoordRepType = PathFilterType::CostFunctionType::CoordRepType;
137+
using PathIteratorType = itk::PathIterator< OutputImageType, PathType >;
139138

140139
// Get filename arguments
141140
unsigned int argi = 1;
@@ -150,8 +149,7 @@ ImageType::Pointer speed = reader->GetOutput();
150149
speed->DisconnectPipeline();
151150

152151
// Create interpolator
153-
typedef itk::LinearInterpolateImageFunction<ImageType, CoordRepType>
154-
InterpolatorType;
152+
using InterpolatorType = itk::LinearInterpolateImageFunction<ImageType, CoordRepType>;
155153
InterpolatorType::Pointer interp = InterpolatorType::New();
156154

157155
// Create cost function
@@ -160,7 +158,7 @@ PathFilterType::CostFunctionType::Pointer cost =
160158
cost->SetInterpolator( interp );
161159

162160
// Create optimizer
163-
typedef itk::RegularStepGradientDescentOptimizer OptimizerType;
161+
using OptimizerType = itk::RegularStepGradientDescentOptimizer;
164162
OptimizerType::Pointer optimizer = OptimizerType::New();
165163
optimizer->SetNumberOfIterations( 1000 );
166164
optimizer->SetMaximumStepLength( 0.5 );
@@ -181,7 +179,7 @@ end[0] = 100; end[1] = 10;
181179
way1[0] = 10; way1[1] = 10;
182180

183181
// Add path information
184-
typedef PathFilterType::PathInformationType PathInformationType;
182+
using PathInformationType = PathFilterType::PathInformationType;
185183
PathInformationType::Pointer info = PathInformationType::New();
186184
info->SetStartPoint( start );
187185
info->SetEndPoint( end );

include/itkArrivalFunctionToPathFilter.h

Lines changed: 30 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -42,16 +42,16 @@ template <class TFilter>
4242
class ArrivalFunctionToPathCommand : public itk::Command
4343
{
4444
public:
45-
/** Standard class typedefs. */
46-
typedef ArrivalFunctionToPathCommand Self;
47-
typedef itk::Command Superclass;
48-
typedef itk::SmartPointer<Self> Pointer;
45+
/** Standard class type alias. */
46+
using Self = ArrivalFunctionToPathCommand;
47+
using Superclass = itk::Command;
48+
using Pointer = itk::SmartPointer<Self>;
4949

5050
/** Method for creation through the object factory. */
5151
itkNewMacro( Self );
5252

53-
/** Some useful typedefs. */
54-
typedef TFilter FilterType;
53+
/** Some useful type alias. */
54+
using FilterType = TFilter;
5555

5656
/** Get/set the Filter. */
5757
itkSetObjectMacro( Filter, FilterType );
@@ -143,44 +143,44 @@ class ITK_EXPORT ArrivalFunctionToPathFilter :
143143
public ImageToPathFilter<TInputImage,TOutputPath>
144144
{
145145
public:
146-
/** Standard class typedefs. */
147-
typedef ArrivalFunctionToPathFilter Self;
148-
typedef ImageToPathFilter<TInputImage,TOutputPath> Superclass;
149-
typedef SmartPointer<Self> Pointer;
150-
typedef SmartPointer<const Self> ConstPointer;
146+
/** Standard class type alias. */
147+
using Self = ArrivalFunctionToPathFilter;
148+
using Superclass = ImageToPathFilter<TInputImage,TOutputPath>;
149+
using Pointer = SmartPointer<Self>;
150+
using ConstPointer = SmartPointer<const Self>;
151151

152152
/** Run-time type information (and related methods). */
153153
itkTypeMacro(ArrivalFunctionToPathFilter,ImageToPathFilter);
154154

155155
/** Method for creation through the object factory. */
156156
itkNewMacro(Self);
157157

158-
/** Some image typedefs. */
159-
typedef TInputImage InputImageType;
160-
typedef typename InputImageType::Pointer InputImagePointer;
161-
typedef typename InputImageType::ConstPointer InputImageConstPointer;
162-
typedef typename InputImageType::RegionType InputImageRegionType;
163-
typedef typename InputImageType::PixelType InputImagePixelType;
158+
/** Some image type alias. */
159+
using InputImageType = TInputImage;
160+
using InputImagePointer = typename InputImageType::Pointer;
161+
using InputImageConstPointer = typename InputImageType::ConstPointer;
162+
using InputImageRegionType = typename InputImageType::RegionType;
163+
using InputImagePixelType = typename InputImageType::PixelType;
164164

165-
/** Some path typedefs. */
166-
typedef TOutputPath OutputPathType;
167-
typedef typename OutputPathType::Pointer OutputPathPointer;
168-
typedef typename OutputPathType::ConstPointer OutputPathConstPointer;
165+
/** Some path type alias. */
166+
using OutputPathType = TOutputPath;
167+
using OutputPathPointer = typename OutputPathType::Pointer;
168+
using OutputPathConstPointer = typename OutputPathType::ConstPointer;
169169

170170
/** ImageDimension constants. */
171171
static constexpr unsigned int InputImageDimension = InputImageType::ImageDimension;
172172

173-
/** Some convenient typedefs. */
174-
typedef Index< InputImageDimension > IndexType;
175-
typedef ContinuousIndex< double, InputImageDimension > ContinuousIndexType;
176-
typedef Point< double, InputImageDimension > PointType;
177-
typedef ArrivalFunctionToPathCommand< Self > CommandType;
178-
typedef SingleImageCostFunction< InputImageType > CostFunctionType;
179-
typedef SingleValuedNonLinearOptimizer OptimizerType;
180-
typedef RegularStepGradientDescentOptimizer DefaultOptimizerType;
173+
/** Some convenient type alias. */
174+
using IndexType = Index< InputImageDimension >;
175+
using ContinuousIndexType = ContinuousIndex< double, InputImageDimension >;
176+
using PointType = Point< double, InputImageDimension >;
177+
using CommandType = ArrivalFunctionToPathCommand< Self >;
178+
using CostFunctionType = SingleImageCostFunction< InputImageType >;
179+
using OptimizerType = SingleValuedNonLinearOptimizer;
180+
using DefaultOptimizerType = RegularStepGradientDescentOptimizer;
181181

182182
/** The points are in vectors to support extended "nodes" */
183-
typedef std::vector < PointType> PointsContainerType;
183+
using PointsContainerType = std::vector < PointType>;
184184

185185
/** Get/set the Optimizer. */
186186
itkSetObjectMacro( Optimizer, OptimizerType );

include/itkIterateNeighborhoodOptimizer.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,11 @@ class MinimalPathExtraction_EXPORT IterateNeighborhoodOptimizer :
4141
public SingleValuedNonLinearOptimizer
4242
{
4343
public:
44-
/** Standard class typedefs. */
45-
typedef IterateNeighborhoodOptimizer Self;
46-
typedef SingleValuedNonLinearOptimizer Superclass;
47-
typedef SmartPointer<Self> Pointer;
48-
typedef SmartPointer<const Self> ConstPointer;
44+
/** Standard class type alias. */
45+
using Self = IterateNeighborhoodOptimizer;
46+
using Superclass = SingleValuedNonLinearOptimizer;
47+
using Pointer = SmartPointer<Self>;
48+
using ConstPointer = SmartPointer<const Self>;
4949

5050
/** Method for creation through the object factory. */
5151
itkNewMacro( Self );
@@ -92,7 +92,7 @@ class MinimalPathExtraction_EXPORT IterateNeighborhoodOptimizer :
9292
/** Get/set the nieghborhood size (in physical space).
9393
* The default is [1.0,1.0] and MUST be specified for all 3-D images
9494
* and 2-D images with non-unity spacing. */
95-
typedef Array<double> NeighborhoodSizeType;
95+
using NeighborhoodSizeType = Array<double>;
9696
itkSetMacro( NeighborhoodSize, NeighborhoodSizeType );
9797
itkGetConstReferenceMacro( NeighborhoodSize, NeighborhoodSizeType );
9898

include/itkPhysicalCentralDifferenceImageFunction.h

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -50,39 +50,38 @@ class ITK_EXPORT PhysicalCentralDifferenceImageFunction :
5050
/** Dimension underlying input image. */
5151
static constexpr unsigned int ImageDimension = TInputImage::ImageDimension;
5252

53-
/** Standard class typedefs. */
54-
typedef PhysicalCentralDifferenceImageFunction Self;
55-
typedef ImageFunction<TInputImage,
53+
/** Standard class type alias. */
54+
using Self = PhysicalCentralDifferenceImageFunction;
55+
using Superclass = ImageFunction<TInputImage,
5656
CovariantVector<double,
5757
itkGetStaticConstMacro(ImageDimension)>,
58-
TCoordRep> Superclass;
59-
typedef SmartPointer<Self> Pointer;
60-
typedef SmartPointer<const Self> ConstPointer;
58+
TCoordRep>;
59+
using Pointer = SmartPointer<Self>;
60+
using ConstPointer = SmartPointer<const Self>;
6161

6262
/** Run-time type information (and related methods). */
6363
itkTypeMacro(PhysicalCentralDifferenceImageFunction, ImageFunction);
6464

6565
/** Method for creation through the object factory. */
6666
itkNewMacro(Self);
6767

68-
/** InputImageType typedef support. */
69-
typedef TInputImage InputImageType;
68+
/** InputImageType type alias support. */
69+
using InputImageType = TInputImage;
7070

7171
/** OutputType typdef support. */
72-
typedef typename Superclass::OutputType OutputType;
72+
using OutputType = typename Superclass::OutputType;
7373

74-
/** Index typedef support. */
75-
typedef typename Superclass::IndexType IndexType;
74+
/** Index type alias support. */
75+
using IndexType = typename Superclass::IndexType;
7676

77-
/** ContinuousIndex typedef support. */
78-
typedef typename Superclass::ContinuousIndexType ContinuousIndexType;
77+
/** ContinuousIndex type alias support. */
78+
using ContinuousIndexType = typename Superclass::ContinuousIndexType;
7979

80-
/** Point typedef support. */
81-
typedef typename Superclass::PointType PointType;
80+
/** Point type alias support. */
81+
using PointType = typename Superclass::PointType;
8282

83-
/** Linear interpolate function typedef. */
84-
typedef LinearInterpolateImageFunction<TInputImage, TCoordRep>
85-
InterpolateImageFunctionType;
83+
/** Linear interpolate function type alias. */
84+
using InterpolateImageFunctionType = LinearInterpolateImageFunction<TInputImage, TCoordRep>;
8685

8786
/** Set the input image.
8887
* \warning this method caches BufferedRegion information.

include/itkSingleImageCostFunction.h

Lines changed: 21 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -53,56 +53,52 @@ class ITK_EXPORT SingleImageCostFunction :
5353
public SingleValuedCostFunction
5454
{
5555
public:
56-
/** Standard class typedefs. */
57-
typedef SingleImageCostFunction Self;
58-
typedef SingleValuedCostFunction Superclass;
59-
typedef SmartPointer<Self> Pointer;
60-
typedef SmartPointer<const Self> ConstPointer;
56+
/** Standard class type alias. */
57+
using Self = SingleImageCostFunction;
58+
using Superclass = SingleValuedCostFunction;
59+
using Pointer = SmartPointer<Self>;
60+
using ConstPointer = SmartPointer<const Self>;
6161

6262
/** Method for creation through the object factory. */
6363
itkNewMacro(Self);
6464

6565
/** Run-time type information (and related methods). */
6666
itkTypeMacro( SingleImageCostFunction, SingleValuedCostFunction );
6767

68-
/** MeasureType typedef.
68+
/** MeasureType type alias.
6969
* It defines a type used to return the cost function value. */
70-
typedef typename Superclass::MeasureType MeasureType;
70+
using MeasureType = typename Superclass::MeasureType;
7171

72-
/** DerivativeType typedef.
72+
/** DerivativeType type alias.
7373
* It defines a type used to return the cost function derivative. */
74-
typedef typename Superclass::DerivativeType DerivativeType;
74+
using DerivativeType = typename Superclass::DerivativeType;
7575

76-
/** ParametersType typedef.
76+
/** ParametersType type alias.
7777
* It defines a position in the optimization search space. */
78-
typedef typename Superclass::ParametersType ParametersType;
78+
using ParametersType = typename Superclass::ParametersType;
7979

8080
/** Type of the Image. */
81-
typedef TImage ImageType;
82-
typedef typename TImage::PixelType ImagePixelType;
83-
typedef typename ImageType::ConstPointer ImageConstPointer;
81+
using ImageType = TImage;
82+
using ImagePixelType = typename TImage::PixelType;
83+
using ImageConstPointer = typename ImageType::ConstPointer;
8484

8585
/** Constant for the image dimension */
8686
static constexpr unsigned int ImageDimension = ImageType::ImageDimension;
8787

8888
/** Type used for representing point components */
89-
typedef Superclass::ParametersValueType CoordRepType;
89+
using CoordRepType = Superclass::ParametersValueType;
9090

9191
/** Type for locations */
92-
typedef Index< ImageDimension > IndexType;
93-
typedef Point< CoordRepType, ImageDimension > PointType;
94-
typedef ContinuousIndex< CoordRepType, ImageDimension >
95-
ContinuousIndexType;
92+
using IndexType = Index< ImageDimension >;
93+
using PointType = Point< CoordRepType, ImageDimension >;
94+
using ContinuousIndexType = ContinuousIndex< CoordRepType, ImageDimension >;
9695

9796
/** Type of the Interpolator class */
98-
typedef InterpolateImageFunction< ImageType, CoordRepType >
99-
InterpolatorType;
100-
typedef LinearInterpolateImageFunction< ImageType, CoordRepType >
101-
DefaultInterpolatorType;
97+
using InterpolatorType = InterpolateImageFunction< ImageType, CoordRepType >;
98+
using DefaultInterpolatorType = LinearInterpolateImageFunction< ImageType, CoordRepType >;
10299

103100
/** Type of the GradientImageFunction class */
104-
typedef PhysicalCentralDifferenceImageFunction< ImageType, CoordRepType >
105-
GradientImageFunctionType;
101+
using GradientImageFunctionType = PhysicalCentralDifferenceImageFunction< ImageType, CoordRepType >;
106102

107103
/** Get/set the Interpolator. */
108104
itkSetObjectMacro( Interpolator, InterpolatorType );

include/itkSpeedFunctionPathInformation.h

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -53,11 +53,11 @@ class SpeedFunctionPathInformation :
5353
public LightObject
5454
{
5555
public:
56-
/** Standard class typedefs. */
57-
typedef SpeedFunctionPathInformation Self;
58-
typedef LightObject Superclass;
59-
typedef SmartPointer<Self> Pointer;
60-
typedef SmartPointer<const Self> ConstPointer;
56+
/** Standard class type alias. */
57+
using Self = SpeedFunctionPathInformation;
58+
using Superclass = LightObject;
59+
using Pointer = SmartPointer<Self>;
60+
using ConstPointer = SmartPointer<const Self>;
6161

6262

6363
/** Run-time type information (and related methods). */
@@ -66,9 +66,9 @@ class SpeedFunctionPathInformation :
6666
/** Method for creation through the object factory. */
6767
itkNewMacro(Self);
6868

69-
/** Some point typedefs. */
70-
typedef TPoint PointType;
71-
typedef std::vector< PointType > PointsContainerType;
69+
/** Some point type alias. */
70+
using PointType = TPoint;
71+
using PointsContainerType = std::vector< PointType >;
7272

7373
void ClearInfo();
7474

0 commit comments

Comments
 (0)