Skip to content

[QUESTION]: BUG or FEATURE? overload_cast doesn't work for auto deduced return type functions templates #3174

@junduck

Description

@junduck

Required prerequisites

Problem description

When an overloaded member function's return type is auto deduced, and the class is a template, overload_cast fails to deduce return type.

  In file included from /tmp/pip-build-env-n9g56e9j/overlay/lib/python3.9/site-packages/pybind11/include/pybind11/pytypes.h:12,
                   from /tmp/pip-build-env-n9g56e9j/overlay/lib/python3.9/site-packages/pybind11/include/pybind11/cast.h:13,
                   from /tmp/pip-build-env-n9g56e9j/overlay/lib/python3.9/site-packages/pybind11/include/pybind11/attr.h:13,
                   from /tmp/pip-build-env-n9g56e9j/overlay/lib/python3.9/site-packages/pybind11/include/pybind11/pybind11.h:23,
                   from src/doublePet.cpp:1:
  /tmp/pip-build-env-n9g56e9j/overlay/lib/python3.9/site-packages/pybind11/include/pybind11/detail/common.h:843:20: note: candidate: ‘template<class Return, class Class> constexpr decltype (pmf) pybind11::detail::overload_cast_impl<Args>::operator()(Return (Class::*)(Args ...) const, std::true_type) const [with Return = Return; Class = Class; Args = {const std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >&}]’
    843 |     constexpr auto operator()(Return (Class::*pmf)(Args...) const, std::true_type) const noexcept
        |                    ^~~~~~~~
  /tmp/pip-build-env-n9g56e9j/overlay/lib/python3.9/site-packages/pybind11/include/pybind11/detail/common.h:843:20: note:   template argument deduction/substitution failed:
  src/doublePet.cpp:37:59: note:   types ‘Return (Class::)(const std::__cxx11::basic_string<char>&) const’ and ‘double (Pet<double>::)(double)’ have incompatible cv-qualifiers
     37 |         .def("set", py::overload_cast<const std::string &>(&doublePet::set), "Set the pet's name");
        |                     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~
  src/doublePet.cpp:37:59: note:   candidate expects 2 arguments, 1 provided

Reproducible example code

#include "pybind11/pybind11.h"
namespace py = pybind11;

template <typename T>
struct Pet
{
    Pet(const std::string &name, T age) : name(name), age(age) {}

    auto set(T age_)
    {
        age = age_;
        return age;
    }
    auto set(const std::string &name_)
    {
        name = name_;
        return name;
    }

    std::string name;
    T age;
};
using doublePet = Pet<double>;

void bind_pet(py::module &m)
{
    py::class_<doublePet>(m, "doublePet")
        .def("set", py::overload_cast<double>(&doublePet::set), "Set the pet's age")
        .def("set", py::overload_cast<const std::string &>(&doublePet::set), "Set the pet's name");
}

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions