Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add constexpr to compile time const variables #5086

Merged
merged 6 commits into from
Dec 18, 2024

Conversation

hjmjohnson
Copy link
Member

IDE can identify these cases, but code changes are implemented manually

If statement with compile-time constant can be replaced with "if constexpr"

    REPLACE: (^ *)const *([a-zA-Z0-9_:]* [0-9a-zA-Z_]* = *[\+\-e0-9\.]+;)
       WITH: $1constexpr $2
    
    REPLACE: (^ *)const (\w* \w*\{\}\;)
       WITH: $1constexpr $2

PR Checklist

@hjmjohnson hjmjohnson self-assigned this Dec 17, 2024
@hjmjohnson hjmjohnson added the type:Style Style changes: no logic impact (indentation, comments, naming) label Dec 17, 2024
@hjmjohnson hjmjohnson added this to the ITK 6.0 Beta 1 milestone Dec 17, 2024
@github-actions github-actions bot removed the type:Style Style changes: no logic impact (indentation, comments, naming) label Dec 17, 2024
@blowekamp
Copy link
Member

@hjmjohnson Great work on these style improvements 😍

As I was working on the VectorImage bug, I was looking at the NumericTraits classes. You may want to look at these classes and see if methods like SetLength and others could be made constexpr. I think there are large potential for performance improvements using "if constexpr" statements in tight filter loops. I am sure your queue is filled with items, but just wanted to mention this for your consideration.

Implements detection of local variables which could be declared as const but
are not. Declaring variables as const is recommended by many coding
guidelines, such as: ES.25 from the C++ Core Guidelines.

cd ${BLDDIR}
run-clang-tidy.py -extra-arg=-D__clang__ -checks=-*,misc-const-correctness -header-filter=.* -fix
IDE can identify these cases, but code changes are implemented manually

REPLACE: (^ *)const *([a-zA-Z0-9_:]* [0-9a-zA-Z_]* = *[\+\-e0-9\.]+;)
   WITH: $1constexpr $2

REPLACE: (^ *)const (\w* \w*\{\}\;)
   WITH: $1constexpr $2
If statement with compile-time constant can be
replaced with "if constexpr"
The test for epsilon > 0 is always true because
epsilon is a constexpr greater than 0.
[CTest: warning matched] /Users/builder/externalModules/Filtering/ImageCompare/test/itkSimilarityIndexImageFilterTest.cxx:63:18:
warning: result of comparison of 0 <= unsigned expression is always true [-Wtautological-unsigned-zero-compare]
   63 |       if (lower1 <= count && count <= upper1)
      |           ~~~~~~ ^  ~~~~~
[CTest: warning suppressed] 1 warning generated.
Windows build failed when making an unused variable constexpr.
Modules\Core\Common\test\itkVectorContainerTest.cxx(54): error C2127:
'p_null': illegal initialization of 'constexpr' entity with a
non-constant expression

Remove the unused variable to avoid the problem.
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.

🔥

@hjmjohnson hjmjohnson merged commit 74916a5 into InsightSoftwareConsortium:master Dec 18, 2024
15 checks passed
@hjmjohnson hjmjohnson deleted the add-constexpr branch December 18, 2024 01:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants