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

Use unique_ptr for Pimple Transform #1922

Merged
merged 1 commit into from Jun 1, 2023

Conversation

blowekamp
Copy link
Member

@blowekamp blowekamp commented May 1, 2023

Remove raw pointer usage for the pimple transform with unique_ptr which enforces the logic of ownership to the heap allocated object.

@blowekamp blowekamp changed the title Use unique_ptr for Transform interface Use unique_ptr for Pimple Image and Transform May 2, 2023
@blowekamp blowekamp requested review from zivy and N-Dekker May 2, 2023 12:44
@@ -80,7 +80,7 @@ class SITKCommon_EXPORT AffineTransform

protected:

void SetPimpleTransform( PimpleTransformBase *pimpleTransform ) override;
void SetPimpleTransform(std::unique_ptr<PimpleTransformBase> &&pimpleTransform ) override;
Copy link
Collaborator

Choose a reason for hiding this comment

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

Thanks for asking me to have a look!

The && are not really necessary here, you may just pass the unique_ptr "by value". Example (https://godbolt.org/z/h4cs6fzWo):

void MyFunc(std::unique_ptr<MyObject> myObj)
{
}

int main()
{
    MyFunc(std::make_unique<MyObject>());
}

Of course, you can still use &&, but pass by value is more common, for unique_ptr. My two cents.

Copy link
Member Author

Choose a reason for hiding this comment

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

Good to know that is the conventions. The move is still required with the following:

int main()
{
    auto uptr = std::make_unique<MyObject>();
    MyFunc(std::move(uptr));
}

I suppose there is no more cost to moving the unique_ptr object that just copying a ptr.

@blowekamp blowekamp marked this pull request as ready for review May 3, 2023 17:44
@blowekamp
Copy link
Member Author

@N-Dekker After following your suggestion of changing the r-value reference parameter to pass by value There are some compilation errors on linux system ( before others were canceled ):

https://open.cdash.org/viewBuildError.php?buildid=8664641

/usr/include/c++/9/bits/unique_ptr.h:79:16: error: invalid application of 'sizeof' to incomplete type 'itk::simple::PimpleTransformBase'

Any suggestions?

@blowekamp
Copy link
Member Author

@N-Dekker After following your recommendation of passing std::unique_ptr by value instead of r-value, I are getting errors on most compilers related to the pimple object type being only a forward declaration. Any further suggestions or recommendations?

@N-Dekker
Copy link
Collaborator

@N-Dekker After following your recommendation of passing std::unique_ptr by value instead of r-value, I are getting errors on most compilers related to the pimple object type being only a forward declaration. Any further suggestions or recommendations?

Sorry, I did not know about this problem. I've been trying to reproduce the errors at https://godbolt.org/z/cP7Pas5zT but so far, I don't see them there. You see, I just tried:

class ForwardDeclared;

class MyClass
{
void Func(std::unique_ptr<ForwardDeclared>);
};

Anyway, if there is no other solution, just revert my suggestion to pass std::unique_ptr "by value". 🤷

@blowekamp
Copy link
Member Author

blowekamp commented May 11, 2023

So this case is a bit more interesting. Here is the compilation error reproduced:
https://godbolt.org

If the G function's argument is changed to a r-value reference then the error is no longer there.

Thank you for your response.

@N-Dekker
Copy link
Collaborator

So this case is a bit more interesting. Here is the compilation error reproduced:
https://godbolt.org

It does not show up at my side. Can you please select Share >> Short link in the Godbolt site, and share the link? Or otherwise just copy-paste your code?

@blowekamp
Copy link
Member Author

Correct link: https://godbolt.org/z/7fMaxG9bd

@N-Dekker
Copy link
Collaborator

Correct link: https://godbolt.org/z/7fMaxG9bd

I see, you pass the unique_ptr-to-an-incomplete-type forward from F to G, and in that case, it appears that it must be passed by && reference, instead of by value. This seems to break the rule that unique_ptr must be passed by value, indeed.

Looking at https://www.reddit.com/r/cpp_questions/comments/gpfij5/passing_unique_ptr_by_value_vs_by_reference/ they all say unique_ptr should be passed by value, but it looks like you proved them wrong!

I can still ask around to see if your case is indeed an exception to the rule. But in the mean time, do whatever you need to avoid those compiler errors! 👍

@blowekamp blowekamp changed the title Use unique_ptr for Pimple Image and Transform Use unique_ptr for Pimple Transform May 31, 2023
@N-Dekker
Copy link
Collaborator

Off-topic nit-pick: the C++ idiom I think this is PR about is officially called "Pimpl", either in upper or lower case, but without the letter "e". (See https://en.cppreference.com/w/cpp/language/pimpl or http://wiki.c2.com/?PimplIdiom) So you might still consider replacing any "Pimple" with "Pimpl".

@blowekamp blowekamp merged commit 09081f5 into SimpleITK:master Jun 1, 2023
12 checks passed
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.

None yet

3 participants