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

Adds a description of why we implement invoke as we do #368

Merged
merged 14 commits into from
Dec 12, 2019

Conversation

barcharcraz
Copy link
Member

@barcharcraz barcharcraz commented Dec 10, 2019

Description

I ran across this comment on reddit from our very own @StephanTLavavej about why we need the _IMPLEMENT_INVOKE macro. I thought it was helpful and I had bounced off that particular macro a few times, so I added his explanation as a comment in the code (with some minor editing)

Checklist

Be sure you've read README.md and understand the scope of this repo.

If you're unsure about a box, leave it unchecked. A maintainer will help you.

  • Identifiers in product code changes are properly _Ugly as per
    https://eel.is/c++draft/lex.name#3.1 or there are no product code changes.
  • The STL builds successfully and all tests have passed (must be manually
    verified by an STL maintainer before automated testing is enabled on GitHub,
    leave this unchecked for initial submission).
  • These changes introduce no known ABI breaks (adding members, renaming
    members, adding virtual functions, changing whether a type is an aggregate
    or trivially copyable, etc.).
  • These changes were written from scratch using only this repository,
    the C++ Working Draft (including any cited standards), other WG21 papers
    (excluding reference implementations outside of proposed standard wording),
    and LWG issues as reference material. If they were derived from a project
    that's already listed in NOTICE.txt, that's fine, but please mention it.
    If they were derived from any other project (including Boost and libc++,
    which are not yet listed in NOTICE.txt), you must mention it here,
    so we can determine whether the license is compatible and what else needs
    to be done.

@barcharcraz barcharcraz requested a review from a team as a code owner December 10, 2019 20:05
// it to be constexpr, yet variant visit() is required to be constexpr and have invoke-like behavior. We solve this by
// using a macro to stamp out a public non-constexpr and internal constexpr implementation.

// Now that a Core Language issue affecting constexpr invoke() has been fixed (CWG1581), we may make this unconditional
Copy link
Member

Choose a reason for hiding this comment

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

We wouldn't strengthen unconditionally, we would just make invoke call _C_invoke.

Copy link
Member Author

Choose a reason for hiding this comment

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

I suppose, we'd also eliminate this macro and just expand it in the constexpr case right?

Copy link
Member

Choose a reason for hiding this comment

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

There are throughput considerations here - std::function is throughput-sensitive and it uses invoke(). If invoke() were defined to call _C_invoke(), that would be an unnecessary layer of function calls.

I believe that the best thing to do would be to:

  1. De-macroize this machinery,
  2. Make the _Invoker layer unconditionally constexpr,
  3. Make invoke() conditionally _CONSTEXPR20,
  4. Change apply() and visit() to use the _Invoker layer.

Copy link
Member

Choose a reason for hiding this comment

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

Make the _Invoker layer unconditionally constexpr

Can we do that without CWG-1581?

Copy link
Member Author

Choose a reason for hiding this comment

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

I don't think this conversation is relevant to documenting what's there now right.

Copy link
Member

Choose a reason for hiding this comment

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

This affects your comment "Now that a Core Language issue affecting constexpr std::invoke has been fixed (CWG-1581), we may make this unconditional and remove the macro (which violates the strengthening rule in C++14/17 mode). TRANSITION, P1065R2" which talks about the future.

Copy link
Member Author

Choose a reason for hiding this comment

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

yeah it seems like we may be able to do this without CWG-1581...

Copy link
Member

Choose a reason for hiding this comment

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

yeah it seems like we may be able to do this without CWG-1581

I disagree; getting incorrect behavior from invoke is exactly why CWG-1581 existed in the first place. It's possible that Stephan's implementation, which does not use expression SFINAE, is immune to that problem, but I don't understand this area well enough to make such a firm statement. See http://www.open-std.org/JTC1/SC22/WG21/docs/papers/2018/p1065r0.html for more nitty gritty details.

stl/inc/type_traits Outdated Show resolved Hide resolved
stl/inc/type_traits Outdated Show resolved Hide resolved
stl/inc/type_traits Outdated Show resolved Hide resolved
stl/inc/type_traits Outdated Show resolved Hide resolved
stl/inc/type_traits Outdated Show resolved Hide resolved
stl/inc/type_traits Outdated Show resolved Hide resolved
stl/inc/type_traits Outdated Show resolved Hide resolved
barcharcraz and others added 6 commits December 10, 2019 12:52
Co-Authored-By: Casey Carter <cartec69@gmail.com>
Co-Authored-By: Casey Carter <cartec69@gmail.com>
Co-Authored-By: Casey Carter <cartec69@gmail.com>
Co-Authored-By: Casey Carter <cartec69@gmail.com>
stl/inc/type_traits Outdated Show resolved Hide resolved
Co-Authored-By: Casey Carter <cartec69@gmail.com>
// it to be constexpr, yet variant visit() is required to be constexpr and have invoke-like behavior. We solve this by
// using a macro to stamp out a public non-constexpr and internal constexpr implementation.

// Now that a Core Language issue affecting constexpr invoke() has been fixed (CWG1581), we may make this unconditional
Copy link
Member

Choose a reason for hiding this comment

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

There are throughput considerations here - std::function is throughput-sensitive and it uses invoke(). If invoke() were defined to call _C_invoke(), that would be an unnecessary layer of function calls.

I believe that the best thing to do would be to:

  1. De-macroize this machinery,
  2. Make the _Invoker layer unconditionally constexpr,
  3. Make invoke() conditionally _CONSTEXPR20,
  4. Change apply() and visit() to use the _Invoker layer.

stl/inc/type_traits Outdated Show resolved Hide resolved
stl/inc/type_traits Outdated Show resolved Hide resolved
stl/inc/type_traits Outdated Show resolved Hide resolved
stl/inc/type_traits Outdated Show resolved Hide resolved
stl/inc/type_traits Outdated Show resolved Hide resolved
stl/inc/type_traits Outdated Show resolved Hide resolved
@StephanTLavavej StephanTLavavej merged commit 70e49a0 into microsoft:master Dec 12, 2019
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