-
-
Notifications
You must be signed in to change notification settings - Fork 664
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
STYLE: Remove this->MakeOutput(0)
calls from constructors in Core
#4654
base: master
Are you sure you want to change the base?
STYLE: Remove this->MakeOutput(0)
calls from constructors in Core
#4654
Conversation
Analogue to ITK pull request InsightSoftwareConsortium/ITK#4654 Following C++ Core Guidelines, February 15, 2024, "Don’t call virtual functions in constructors and destructors", https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#Rc-ctor-virtual
this->ProcessObject::SetNumberOfRequiredOutputs(1); | ||
this->ProcessObject::SetNthOutput(0, output.GetPointer()); | ||
this->ProcessObject::SetNthOutput(0, TOutputImage::New().GetPointer()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please add comment like: "Generally MakeOutput(n) should be used, but because a virtual method should not be called in a constructor the output is directly constructed."
It is important to note how things should be done, and why they are not done that way for this case. There have been bugs before related to not using MakeOutput.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the suggestion @blowekamp
There have been bugs before related to not using MakeOutput.
Can you possibly still recall what such a bug looked like? Just for my understanding!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Don't recall the specifics... There are cases when a Process object can have it's outputs removed, and then during execution it needs to make them again.
Maybe f0d570e?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Interesting! In general, I think this->MakeOutput(i)
calls are not necessary, inside a constructor of a class, when the MakeOutput(i)
override of this class unconditionally just does the same OutputType::New()
call for any index value i
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree they are not necessary. I am just suggestion adding documentation as to why the canonical way of creating a new output is not being used e.g documenting the exception case.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So that was a "no" to the request for documentation?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh, sorry Bradley, what do you want there? Something like
// Basically equivalent to SetNthOutput(0, MakeOutput(0)).
this->ProcessObject::SetNthOutput(0, TOutputImage::New().GetPointer())
?
I hope it does not need to be longer than one line of comment. This pull request is intended to simplify the code. The idea is: Just call TOutput::New()
if that does the job!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry, I see your suggestion at https://github.com/InsightSoftwareConsortium/ITK/pull/4654/files#r1596759648 As the idea would be to use New()
, rather that MakeOutput
, in multiple places in the source tree, I would rather have those considerations elsewhere. Maybe in the ITKSoftwareGuide?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What do you mean "As the idea would be to use New(), rather that MakeOutput, in multiple places in the source tree"?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Well, this pull request already proposes to replace three MakeOutput
calls with TOutput::New()
. I don't think it's nice to have an extensive multi-line comment copy-and-pasted multiple times.
Analogue to ITK pull request InsightSoftwareConsortium/ITK#4654 Following C++ Core Guidelines, February 15, 2024, "Don’t call virtual functions in constructors and destructors", https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#Rc-ctor-virtual
I think this pull request may also be merged now, as v5.4.0 has been tagged (#4603 (comment)) |
In those cases, `MakeOutput(0)` just did `OutputType::New()` anyway. This commit avoids unnecessary casts and calls to virtual functions. Following C++ Core Guidelines, February 15, 2024, "Don’t call virtual functions in constructors and destructors", https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#Rc-ctor-virtual
6124014
to
9120566
Compare
|
Good question @blowekamp 😃 This PR specifically addresses cases where there is exactly 1 required output. PR #4688 does not yet address those cases. So it's still open 🤷 |
In those cases,
MakeOutput(0)
just didOutputType::New()
anyway. This commit avoids unnecessary casts and calls to virtual functions.Following C++ Core Guidelines, February 15, 2024, "Don’t call virtual functions in constructors and destructors",
https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#Rc-ctor-virtual
Intended for after the release of ITK v5.4.0