当Boost ::绑定模板函数时,模板参数扣除/替换失败

template argument deduction/substitution failed, when boost::binding a templated function

本文关键字:失败 参数 替换 Boost 绑定 函数      更新时间:2023-10-16

我正在尝试提高绑定特定函数以供以后使用,以下代码是抛出编译错误的是,第一个函数是我要绑定的函数以生成增强功能函数,第二个语句是实际的绑定语句,第三个语句是编写此绑定语句的函数,最后有错误:

template <typename T>
void ValidOperation::CheckDoubleBound(
    diagnostic_updater::DiagnosticStatusWrapper& stat,
    const std::string& name,
    const T& variable_instance,
    T upper_bound_value,
    T lower_bound_value,
    const SeverityLevel severity,
    const std::string& message_success,
    const std::string& message_lower_fail,
    const std::string& message_upper_fail)

和绑定看起来像这样

    auto func = boost::bind(&ValidOperation::CheckDoubleBound<T>,
                            this,
                            _1,
                            name,
                            boost::cref(variable_instance),
                            upper_bound_value,
                            lower_bound_value,
                            severity,
                            message_success,
                            message_upper_fail,
                            message_lower_fail);

此功能内部

template <typename T>
void ValidOperation::AddDoubleBoundConstraint(
    const std::string& name,
    const T& variable_instance,
    const T upper_bound_value,
    const T lower_bound_value,
    const SeverityLevel severity,
    const std::string& message_success,
    const std::string& message_lower_fail,
    const std::string& message_upper_fail)
In instantiation of ‘void triton::ValidOperation::AddDoubleBoundConstraint(const string&, const T&, T, T, triton::SeverityLevel, const string&, const string&, const string&) [with T = double; std::__cxx11::string = std::__cxx11::basic_string<char>]’:
/home/test/src/ValidOperation.cpp:188:42:   required from here
/home/test/src/ValidOperation.cpp:150:28: error: no matching function for call to ‘bind(<unresolved overloaded function type>, triton::ValidOperation*, const boost::arg<1>&, const string&, const boost::reference_wrapper<const double>, const double&, const double&, const triton::SeverityLevel&, const string&, const string&, const string&)’
     auto func = boost::bind(&ValidOperation::CheckDoubleBound<T>,
                 ~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
                             this,
                             ~~~~~
                             _1,
                             ~~~
                             name,
                             ~~~~~
                             boost::cref(variable_instance),
                             ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
                             upper_bound_value,
                             ~~~~~~~~~~~~~~~~~~
                             lower_bound_value,
                             ~~~~~~~~~~~~~~~~~~
                             severity,
                             ~~~~~~~~~
                             message_success,
                             ~~~~~~~~~~~~~~~~
                             message_upper_fail,
                             ~~~~~~~~~~~~~~~~~~~
                             message_lower_fail);
                             ~~~~~~~~~~~~~~~~~~~
In file included from /usr/include/boost/bind.hpp:22:0,
                 from /opt/ros/melodic/include/ros/publisher.h:35,
                 from /opt/ros/melodic/include/ros/node_handle.h:32,
                 from /opt/ros/melodic/include/diagnostic_updater/diagnostic_updater.h:42,
                 from /home/test/include/triton/ValidOperation.h:23,
                 from /home/test/src/ValidOperation.cpp:17:
/usr/include/boost/bind/bind.hpp:1875:5: note: candidate: template<class R, class F> boost::_bi::bind_t<R, F, boost::_bi::list0> boost::bind(F)
     BOOST_BIND(F f)
     ^
/usr/include/boost/bind/bind.hpp:1875:5: note:   template argument deduction/substitution failed:
/home/src/ValidOperation.cpp:150:28: note:   candidate expects 1 argument, 11 provided
     auto func = boost::bind(&ValidOperation::CheckDoubleBound<T>,
                 ~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
                             this,
                             ~~~~~
                             _1,
                             ~~~
                             name,
                             ~~~~~
                             boost::cref(variable_instance),
                             ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
                             upper_bound_value,
                             ~~~~~~~~~~~~~~~~~~
                             lower_bound_value,
                             ~~~~~~~~~~~~~~~~~~
                             severity,
                             ~~~~~~~~~
                             message_success,
                             ~~~~~~~~~~~~~~~~
                             message_upper_fail,
                             ~~~~~~~~~~~~~~~~~~~
                             message_lower_fail);

我正在使用C 14和GCC 7.4.0。

编辑:这起作用

auto func = [this,
                 name,
                 variable_instance,
                 upper_bound_value,
                 lower_bound_value,
                 severity,
                 message_success,
                 message_lower_fail,
                 message_upper_fail](
                    diagnostic_updater::DiagnosticStatusWrapper& stat) {
        this->CheckDoubleBound<T>(stat,
                                  name,
                                  variable_instance,
                                  upper_bound_value,
                                  lower_bound_value,
                                  severity,
                                  message_success,
                                  message_lower_fail,
                                  message_upper_fail);
    };

如果有人可以解释为什么这有效而不是前者?谢谢!

有人向我指出了答案,这就是boost :: bind可以在最大的情况下进行9个参数,而我有十个包括类指针。

https://www.boost.org/doc/libs/1_66_0/libs/bind/doc/doc/html/bind.html#bind.implementation.number_of_arguments