Skip to content

STYLE: Replace static_cast<T>(Dimension) with T{ Dimension }#5324

Merged
thewtex merged 1 commit intoInsightSoftwareConsortium:masterfrom
N-Dekker:Replace-static_cast-Dimension
Apr 25, 2025
Merged

STYLE: Replace static_cast<T>(Dimension) with T{ Dimension }#5324
thewtex merged 1 commit intoInsightSoftwareConsortium:masterfrom
N-Dekker:Replace-static_cast-Dimension

Conversation

@N-Dekker
Copy link
Contributor

@N-Dekker N-Dekker commented Apr 24, 2025

Replaced static_cast<(\w+)>\(([A-Za-z]*Dimension)\) with $1{ $2 }, using regular expressions.

Excluded static_cast<double>(SizeAccumulateDimension), as it is not a compile-time constant expression.

Following C++ Core Guidelines, "Avoid casts"


Unlike static_cast<T>(Dimension), T{ Dimension } checks at compile-time that the dimension can be converted losslessly, for example: https://godbolt.org/z/Eoc9fhnnz

    static constexpr unsigned int SmallDimension{ 2 };
    static constexpr unsigned int LargeDimension{ UINT_MAX };

    return static_cast<int>(SmallDimension); // OK
    return static_cast<int>(LargeDimension); // overflow, but no error!
    return int{ SmallDimension }; // OK, this conversion is safe.
    return int{ LargeDimension }; // Compile error!

Replaced `static_cast<(\w+)>\(([A-Za-z]*Dimension)\)` with `$1{ $2 }`, using
regular expressions.

Excluded `static_cast<double>(SizeAccumulateDimension)` in
Modules/Filtering/ImageStatistics/include/itkAccumulateImageFilter.hxx, as it
is not a compile-time constant expression.

Following C++ Core Guidelines, Oct 3, 2024, "Avoid casts",
https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#es48-avoid-casts
@github-actions github-actions bot added type:Testing Ensure that the purpose of a class is met/the results on a wide set of test cases are correct area:Core Issues affecting the Core module area:Filtering Issues affecting the Filtering module area:IO Issues affecting the IO module area:Registration Issues affecting the Registration module area:Segmentation Issues affecting the Segmentation module type:Style Style changes: no logic impact (indentation, comments, naming) labels Apr 24, 2025
@N-Dekker N-Dekker marked this pull request as ready for review April 24, 2025 11:23
Copy link
Member

@thewtex thewtex left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💯

@thewtex thewtex merged commit e18f336 into InsightSoftwareConsortium:master Apr 25, 2025
16 checks passed
N-Dekker added a commit to N-Dekker/ITK that referenced this pull request May 1, 2025
Replaced `static_cast<(\w+)>\(([A-Z_][A-Z_]+)\)` with `$1{ $2 }`. According to
general C++ naming convention, these all "all caps" identifiers refer to
preprocessor macro's, so these conversions are evaluated at compile-time.

- Follow-up to pull request InsightSoftwareConsortium#5324
commit 31859d3
"STYLE: Replace `static_cast<T>(Dimension)` with `T{ Dimension }`"
N-Dekker added a commit to N-Dekker/ITK that referenced this pull request May 1, 2025
Replaced `static_cast<(\w+)>\(([A-Z_][A-Z_]+)\)` with `$1{ $2 }`. According to
common C++ naming conventions, these all "all caps" identifiers refer to
constants, specified by preprocessor macro's, so these conversions are evaluated
at compile-time.

- Follow-up to pull request InsightSoftwareConsortium#5324
commit 31859d3
"STYLE: Replace `static_cast<T>(Dimension)` with `T{ Dimension }`"
N-Dekker added a commit to N-Dekker/ITK that referenced this pull request May 2, 2025
Replaced `static_cast<(\w+)>\(([A-Z_][A-Z_]+)\)` with `$1{ $2 }`, using regular
expressions. According to common C++ naming conventions, these all "all caps"
identifiers refer to constants, specified by preprocessor macro's, so these
conversions are evaluated at compile-time.

Excluded replacing `static_cast<float>(RAND_MAX)` (as encountered in
"itkComplexConjugateImageAdaptorTest.cxx") because this conversion is narrowing
on some platforms. Specifically, on GCC `RAND_MAX` may be 2147483647, while it
is only 32767 on MSVC2022.

- Follow-up to pull request InsightSoftwareConsortium#5324
commit 31859d3
"STYLE: Replace `static_cast<T>(Dimension)` with `T{ Dimension }`"
dzenanz pushed a commit that referenced this pull request May 12, 2025
Replaced `static_cast<(\w+)>\(([A-Z_][A-Z_]+)\)` with `$1{ $2 }`, using regular
expressions. According to common C++ naming conventions, these all "all caps"
identifiers refer to constants, specified by preprocessor macro's, so these
conversions are evaluated at compile-time.

Excluded replacing `static_cast<float>(RAND_MAX)` (as encountered in
"itkComplexConjugateImageAdaptorTest.cxx") because this conversion is narrowing
on some platforms. Specifically, on GCC `RAND_MAX` may be 2147483647, while it
is only 32767 on MSVC2022.

- Follow-up to pull request #5324
commit 31859d3
"STYLE: Replace `static_cast<T>(Dimension)` with `T{ Dimension }`"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:Core Issues affecting the Core module area:Filtering Issues affecting the Filtering module area:IO Issues affecting the IO module area:Registration Issues affecting the Registration module area:Segmentation Issues affecting the Segmentation module type:Style Style changes: no logic impact (indentation, comments, naming) type:Testing Ensure that the purpose of a class is met/the results on a wide set of test cases are correct

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants