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

Qualify detail::forward_like to avoid conflict with C++23 #4136

Merged
merged 1 commit into from Aug 12, 2022
Merged

Qualify detail::forward_like to avoid conflict with C++23 #4136

merged 1 commit into from Aug 12, 2022

Conversation

StephanTLavavej
Copy link
Contributor

Qualify detail::forward_like to avoid conflict with C++23's feature P2445R1 std::forward_like.

Description

C++23 has added a feature P2445R1 std::forward_like, and MSVC's STL has implemented it by merging microsoft/STL#2974 (which will be available in VS 2022 17.4 Preview 3). When building pybind11 with the updated STL in /std:c++latest mode, there are compiler errors because pybind11 has its own forward_like in a detail namespace. Qualifying the callsites is necessary to avoid ambiguity.

Suggested changelog entry:

Fixed compiler errors when C++23 std::forward_like is available.

C++23 feature: P2445R1 forward_like()
@rwgk
Copy link
Collaborator

rwgk commented Aug 11, 2022

Hi Stephan, could you please post a copy of an error message here?

For future reference, also for people who debug via "google the error message" (like myself).

@StephanTLavavej
Copy link
Contributor Author

@rwgk Sure! Here's the exact error message:

F:\gitP\pybind\pybind11\include\pybind11/stl.h(181,1): error C2668: 'pybind11::detail::forward_like': ambiguous call to overloaded function (compiling source file F:\gitP\pybind\pybind11\tests\test_tagbased_polymorphic.cpp) [F:\gitP\pybind\pybind11\build_amd64\tests\pybind11_tests.vcxproj]
F:\gitP\pybind\pybind11\include\pybind11/stl.h(48,22): message : could be 'std::unique_ptr<Animal,std::default_delete<Animal>> &&pybind11::detail::forward_like<T,std::unique_ptr<Animal,std::default_delete<Animal>>&>(U)' [F:\gitP\pybind\pybind11\build_amd64\tests\pybind11_tests.vcxproj]
          with
          [
              T=std::vector<std::unique_ptr<Animal,std::default_delete<Animal>>,std::allocator<std::unique_ptr<Animal,std::default_delete<Animal>>>>,
              U=std::unique_ptr<Animal,std::default_delete<Animal>> &
          ] (compiling source file F:\gitP\pybind\pybind11\tests\test_tagbased_polymorphic.cpp)
C:\Program Files (x86)\Microsoft Visual Studio\2019\Enterprise\VC\Tools\MSVC\14.29.30133\include\utility(823,29): message : or       'auto &&std::forward_like<T,std::unique_ptr<Animal,std::default_delete<Animal>>&>(_Uty) noexcept' [found using argument-dependent lookup] [F:\gitP\pybind\pybind11\build_amd64\tests\pybind11_tests.vcxproj]
          with
          [
              T=std::vector<std::unique_ptr<Animal,std::default_delete<Animal>>,std::allocator<std::unique_ptr<Animal,std::default_delete<Animal>>>>,
              _Uty=std::unique_ptr<Animal,std::default_delete<Animal>> &
          ] (compiling source file F:\gitP\pybind\pybind11\tests\test_tagbased_polymorphic.cpp)
F:\gitP\pybind\pybind11\include\pybind11/stl.h(181,1): message : while trying to match the argument list '(std::unique_ptr<Animal,std::default_delete<Animal>>)' (compiling source file F:\gitP\pybind\pybind11\tests\test_tagbased_polymorphic.cpp) [F:\gitP\pybind\pybind11\build_amd64\tests\pybind11_tests.vcxproj]
F:\gitP\pybind\pybind11\include\pybind11\pybind11.h(167): message : see reference to function template instantiation 'pybind11::handle pybind11::detail::list_caster<std::vector<std::unique_ptr<Animal,std::default_delete<Animal>>,std::allocator<std::unique_ptr<Animal,std::default_delete<Animal>>>>,Type>::cast<std::vector<std::unique_ptr<Animal,std::default_delete<Animal>>,std::allocator<std::unique_ptr<Animal,std::default_delete<Animal>>>>>(T &&,pybind11::return_value_policy,pybind11::handle)' being compiled [F:\gitP\pybind\pybind11\build_amd64\tests\pybind11_tests.vcxproj]
          with
          [
              Type=std::unique_ptr<Animal,std::default_delete<Animal>>,
              T=std::vector<std::unique_ptr<Animal,std::default_delete<Animal>>,std::allocator<std::unique_ptr<Animal,std::default_delete<Animal>>>>
          ] (compiling source file F:\gitP\pybind\pybind11\tests\test_tagbased_polymorphic.cpp)
[...more template instantiation context...]

(Please ignore how the MSVC paths mention VS 2019; this is an artifact of how our test team runs development builds of the compiler+STL with open-source projects.)

@rwgk rwgk merged commit 2d59b43 into pybind:master Aug 12, 2022
@github-actions github-actions bot added the needs changelog Possibly needs a changelog entry label Aug 12, 2022
@StephanTLavavej StephanTLavavej deleted the forward_like branch August 12, 2022 06:45
@Skylion007
Copy link
Collaborator

@StephanTLavavej Question, does the new std::forward_like have identical behavior to detail::forward_like? If so, we may want to add a ifdef that just aliases it instead of reimplementing (like we do for various other backported std functions like enable_if_t etc... in detail/common.h).

@StephanTLavavej
Copy link
Contributor Author

@Skylion007 They don't appear to be identical. std::forward_like has a "copy const" step that detail::forward_like lacks:

template <typename T, typename U>
using forwarded_type = conditional_t<std::is_lvalue_reference<T>::value,
remove_reference_t<U> &,
remove_reference_t<U> &&>;
/// Forwards a value U as rvalue or lvalue according to whether T is rvalue or lvalue; typically
/// used for forwarding a container's elements.
template <typename T, typename U>
constexpr forwarded_type<T, U> forward_like(U &&u) {
return std::forward<detail::forwarded_type<T, U>>(std::forward<U>(u));
}

std::forward_like may still supersede it, but that would depend on usage.

@henryiii henryiii removed the needs changelog Possibly needs a changelog entry label Oct 20, 2022
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

4 participants