wjakob / nanobind

nanobind: tiny and efficient C++/Python bindings
BSD 3-Clause "New" or "Revised" License
2.14k stars 161 forks source link

Rename `forward_like` to avoid conflict with STL #475

Closed yosh-matsuda closed 3 months ago

yosh-matsuda commented 3 months ago

When nanobind user code enables C++23, I found that nanobind::detail::forward_like has a naming conflict with std::forward_like in STL due to ADL.

The situation is like this:

#include <utility>

namespace nanobind {
    template <typename T, typename U>
    auto forward_like(U&& t) { return t; }

    template <typename T>
    auto func(T&& t) { return forward_like<T>(t); }
}

int main() {
    auto p = std::pair(0, 0);
    nanobind::func(p);
    return 0;
}
x86-64 clang 17.0.1
x86-64 clang 17.0.1
-std=c++23 -stdlib=libc++ -Wall -Wextra 
123
<Compilation failed>

# For more information see the output window
x86-64 clang 17.0.1 - cached
<source>:8:31: error: call to 'forward_like' is ambiguous
    8 |     auto func(T&& t) { return forward_like<T>(t); }
      |                               ^~~~~~~~~~~~~~~

In particular, this will potentially occur when casting std object to a Python object using the nanobind STL caster.
The simplest solution is to rename forward_like (in this PR). We can also avoid ADL in other ways.

wjakob commented 3 months ago

Fair enough, thanks!