Improve Return for move-only return types. #330
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Now the object passed to
Return(o)
is always move-returned if it is passed by rvalue reference, even if it can by copied. The goal is that if the user pass it bystd::move
they probably don't want it to be copied (maybe it can increase a counter somewhere and they don't want it to) and it will also fix issues with types where you can't properly detect if they're copyable or not likestd::vector
.Tests should be added to ensure this behavior (object is not copied even if it has a copy constructor).
@malcolmdavey Do you think this behavior is OK or do you see an issue with it ?
This is only supported by the
Return
function that takes a single object, it should be supported byReturn(o1, o2, o3)
.A very strange thing happened while implementing that feature, it seems the
template <typename U = T>
trick doesn't work well withstd::is_reference
: https://godbolt.org/z/hjfKaq1dWThe compiler complains that
func(T&& t)
conflicts withfunc(const T& t)
. It's true that they conflict, becauseT = int&
so once we replace T we get something likefunc(T & && t)
andfunc(T & const & t)
which collapse tofunc(T & t)
andfunc(T & t)
. The issue is thatfunc(T&& t)
should be disabled by SFINAE becauseT
is a reference, so it shouldn't cause an error, and it's what happen when usingstd::is_copy_constructible
, so why doesn't it work withstd::is_reference
?Because this trick doesn't work I used the "inherit partially specialized class" method instead.
Should fix #327.