【问题标题】:template argument deduction/substitution failed, when boost::binding a templated function当 boost::binding 模板化函数时,模板参数推导/替换失败
【发布时间】:2019-07-03 22:23:40
【问题描述】:

我正在尝试增强绑定特定函数以供以后使用,以下代码是引发编译错误的原因第一个函数是我尝试绑定以生成增强函数的函数,第二个语句是实际的bind 语句,第三个语句是这个 bind 语句写在里面的函数,最后有错误:

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 方面的专家(只用过几次)但是为什么你的模板是参考? (在 boost::bind(&ValidOperation::CheckDoubleBound )
  • 这些变量:boost::cref(variable_instance), upper_bound_value, lower_bound_value都是同一个类型吗?
  • 是的,这三个变量是同一类型的。
  • 我不是在这里调用函数,只是创建它以供以后调用。

标签: c++ c++11 templates boost bind


【解决方案1】:

有人指出我的答案,就是 boost::bind 最多可以接受 9 个参数,而我有 10 个参数,包括类指针。

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-04-28
    • 2014-09-22
    • 1970-01-01
    • 1970-01-01
    • 2011-07-28
    • 1970-01-01
    • 2019-07-31
    • 2014-05-19
    相关资源
    最近更新 更多