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

<functional>: Fixes around not_fn #4057

Merged
merged 2 commits into from
Oct 4, 2023

Conversation

frederick-vs-ja
Copy link
Contributor

Make the return type (_Not_fn) perfect forwarding call wrapper since C++20, which is a change in WG21-P0356R5.

Currently this PR leaves _Not_fn as-is in C++17 mode, because the return type was nearly fully specified in C++17 (N4659 [func.not_fn]/1), which means that

  1. due to the absence of deleted operator() overloads, an invocation to a non-const value of the return type might select a const overload, and
  2. due to the use of decltype(!declval<invoke_result_t<...>>()), if the operator! takes a non-movable return type of std::invoke(args...) by value, the return type of operator() would be ill-formed, while !std::invoke(args...) being well-formed.

In C++20 the first case should be ill-formed (in an SFINAE-friendly way), and the second case should be well-formed.

Also make not_fn reject non-move-constructible types. Fixes #4048.

I don't know why call wrappers (except for std::function) are still required to be Cpp17MoveConstructible in C++17 and later. It seems that the restrictions can be lifted.

1. Make the return type (`_Not_fn`) perfect forwarding call wrapper
since C++20, which is a change in WG21-P0356R5.

2. Make `not_fn` reject non-move-constructible types.
@frederick-vs-ja frederick-vs-ja requested a review from a team as a code owner September 28, 2023 16:41
@CaseyCarter CaseyCarter added the bug Something isn't working label Sep 28, 2023
@hewillk
Copy link
Contributor

hewillk commented Sep 28, 2023

You should probably also add a static_assert for is_constructible_v<FD, F> to avoid hard errors in the function body:

struct OnlyMovableFun {
  OnlyMovableFun() = default;
  OnlyMovableFun(const OnlyMovableFun&) = delete;
  OnlyMovableFun(OnlyMovableFun&&) = default;
  bool operator()(auto) const;
};

int main() {
  OnlyMovableFun f;
  auto nf = std::not_fn(f); // should be static_assert?
}

@CaseyCarter
Copy link
Member

You should probably also add a static_assert for is_constructible_v<FD, F> to avoid hard errors in the function body:

That wouldn't "avoid hard errors", it would transform a hard error into a different hard error.

@StephanTLavavej StephanTLavavej self-assigned this Sep 29, 2023
@StephanTLavavej StephanTLavavej removed their assignment Oct 3, 2023
@StephanTLavavej
Copy link
Member

Thanks - this compile-time change is low-risk enough that I think we need only one maintainer approval.

@StephanTLavavej StephanTLavavej self-assigned this Oct 3, 2023
@StephanTLavavej
Copy link
Member

I'm mirroring this to the MSVC-internal repo - please notify me if any further changes are pushed.

@StephanTLavavej StephanTLavavej merged commit 8d44ff0 into microsoft:main Oct 4, 2023
35 checks passed
@StephanTLavavej
Copy link
Member

Thanks for noticing and fixing this issue in not_fn! 🛠️ 🎉 😻

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging this pull request may close these issues.

<functional>: std::not_fn can accept non-movable function
4 participants