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

Avoid _Iter_diff_t in parameters of std function templates #4249

Merged
merged 4 commits into from
Dec 14, 2023

Conversation

CaseyCarter
Copy link
Member

@CaseyCarter CaseyCarter commented Dec 7, 2023

_Iter_diff_t<T> is typename iterator_traits<T>::difference_type in C++17-and-earlier, and iter_difference_t<T> in C++20-and-later. We use it for the parameter types of a few function templates that are specified to take the former. This is (1) nicely shorter to spell than the long iterator_traits type, and (2) possibly a bit cheaper to compile if we can avoid instantiating iterator_traits. I convinced myself the difference probably wasn't observable and we made this change hoping to at least see if it would break anything.

Four years later, we now know what will break: subsumption. If I pull e.g. std::next into a namespace with a using declaration and overload it with constrained function template taking the standard-specified parameters I'll get overload ambiguity on our implementation. The parameter mapping (the transformation from the template parameters to the function parameter types) differs in the two overloads so the constrained overload isn't clearly more specialized.

This PR changes all function template parameters _Iter_diff_t<meow> to typename iterator_traits<meow>::difference_type. I still believe that the difference isn't observable in return types, so I've left them alone. I don't think we're likely to regress this, so I've added no test coverage. Shout - or you know, leave a review comment - if you disagree.

Fixes DevCom-10532126 / VSO-1925201 / AB#1925201.

`_Iter_diff_t<T>` is `typename iterator_traits<T>::difference_type` in C++17-and-earlier, and `iter_difference_t<T>` in C++20-and-later. We use it for the parameter types of a few function templates that are specified to take the former. This is (1) nicely shorter to spell than the long `iterator_traits` type, and (2) possibly a bit cheaper to compile if we can avoid instantiating `iterator_traits`. I convinced myself the difference probably wasn't observable and we made this change hoping to at least see if it would break anything.

Four years later, we now know what will break: subsumption. If I pull e.g. `std::next` into a namespace with a `using` declaration and overload it with constrained function template taking the standard-specified parameters I'll get overload ambiguity on our implementation. The parameter mapping (the transformation from the template parameters to the function parameter types) differs in the two overloads so the constrained overload isn't clearly more specialized.

This PR changes all function template parameters `_Iter_diff_t<meow>` to `typename iterator_traits<meow>::difference_type`. I still believe that the difference isn't observable in return types, so I've left them alone.

There are also some non-local clang-format changes in `<xutility>` that I won't try to explain.
@CaseyCarter CaseyCarter added the bug Something isn't working label Dec 7, 2023
@CaseyCarter CaseyCarter requested a review from a team as a code owner December 7, 2023 17:22
@CaseyCarter
Copy link
Member Author

Me: "There's some weird formatting changes, dunno why clang-format wants them."
clang-format: "WTF with these formatting changes??!?"
Me: ...

@StephanTLavavej
Copy link
Member

This seems tempting to regress during maintenance without test coverage - it's the sort of "why are we saying verbose thing when we could use fewer words" that seems good to cleanup. So I'd like to see test coverage added for this.

stl/inc/algorithm Show resolved Hide resolved
tests/std/test.lst Outdated Show resolved Hide resolved
@StephanTLavavej StephanTLavavej self-assigned this Dec 14, 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 9e74b7e into microsoft:main Dec 14, 2023
35 checks passed
@StephanTLavavej
Copy link
Member

Thanks for making a difference! 😹 🚀 🎉

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.

2 participants