C++ boost函数重载模板

标签 c++ function boost

我无法弄清楚为什么这个段给出 Unresolved 重载函数错误(gcc 版本 4.3.4(Debian 4.3.4-6)):

#include <algorithm>
#include <boost/function.hpp>

// this does not work
int main1()
{
    typedef boost::function<const int&(const int&, const int&)> max;
    max m(&std::max<int>);
}

// this does not work
int main2() {
    typedef boost::function2<const int&, const int&, const int&> max;
    max m(static_cast<max>(&std::max<int>));
}

你能帮帮我吗,谢谢

test.cpp: In function âint main()â:
test.cpp:7: error: no matching function for call to âboost::function2<const int&, const int&, const int&>::function2(<unresolved overloaded function type>)â
/usr/include/boost/function/function_template.hpp:747: note: candidates are: boost::function2<R, T1, T2>::function2(const boost::function2<R, T1, T2>&) [with R = const int&, T0 = const int&\
, T1 = const int&]
/usr/include/boost/function/function_template.hpp:739: note:                 boost::function2<R, T1, T2>::function2(boost::function2<R, T1, T2>::clear_type*) [with R = const int&, T0 = cons\
t int&, T1 = const int&]
/usr/include/boost/function/function_template.hpp:707: note:                 boost::function2<R, T1, T2>::function2() [with R = const int&, T0 = const int&, T1 = const int&]

最大/最小值定义为

  template<typename _Tp>
    inline const _Tp&
    max(const _Tp& __a, const _Tp& __b)
    {
      // concept requirements
      __glibcxx_function_requires(_LessThanComparableConcept<_Tp>)
      //return  __a < __b ? __b : __a;
      if (__a < __b)
        return __b;
      return __a;
    }

我已经尝试了各种模板显式实例化,但似乎没有任何效果。同样的问题出现在 g++ 4.1 而不是 ICC

这行得通

#include <algorithm>
#include <boost/function.hpp>

namespace std_ {
    template<typename _Tp>
    inline const _Tp&
    max(const _Tp& __a, const _Tp& __b)
    {
        // concept requirements
        //return  __a < __b ? __b : __a;
        if (__a < __b)
            return __b;
        return __a;
    }
}

int main()
{
    typedef const int &T;
    typedef boost::function<T(T,T)> min_;
    //typedef const int&(*min_)(const int&, const int&);
    min_ m(::std_::max<int>);
}

还有这个

#include <algorithm>
#include <boost/function.hpp>

int main()
{
    //typedef const int &T;
    //typedef boost::function<T(T,T)> min_;
    typedef const int&(*min_)(const int&, const int&);
    min_ m(::std::max<int>);
}

最佳答案

更新:这是一个 gcc 错误,已在 gcc >=4.4 中修复。 bugzilla .另外,用简化的测试用例修改了我的答案。

这个问题有两个组成部分:boost::function 采用函数指针的方式和 gcc 错误。

boost::function - 您在问题中列出的错误消息有些奇怪;没有候选构造函数可以接受任何类似函数地址的东西。深入研究 boost::function src,相关的构造函数是(省略 enable_if 参数):

template<typename Functor>
function(Functor f) : base_type(f) {}

因此 boost::function 根本无法帮助您指定函数指针的类型;如果函数被重载,则必须转换地址以指定其类型。如果使用重载函数地址,则无法实例化上述模板,因此相应的构造函数不会出现在错误消息中。

gcc 错误 - 如果您再次查看 STL_algobase.h header ,您会看到有两个名为 max 的模板,一个是双参数版本,一个是单参数版本。不过,这对您的代码来说应该不是问题,对吧?术语&max<int>应该实例化单个参数版本并获取其地址。然而,事实并非如此。您可以在简化的(无 header )测试用例中看到问题:

template <class T>
const T& max(const T& x, const T& y){
   return x > y ? x : y;
}

template <class T, class C>
const T& max(const T& x, const T& y, C comp){
   return comp(x, y) ? y : x;

}

template <class R, class A0, class A1>
struct functor{
   template <class F>
   functor(F f) : f(f) {}
   R (*f)(A0, A1);
};

int main(void){
   functor<const int&, const int&, const int&> func(&max<int>);
   return 0;
}

以上代码的结果是 unresolved overloaded function type使用 gcc 4.3.4。解决方法是删除 template <class T, class C> max(...){...}定义或添加 static_cast<const int& (*)(const int&, const int&)>(...)围绕函数地址。我猜这个问题与标准指定的部分显式参数规范的不正确应用有关。它允许您省略尾随模板参数来执行诸如指定返回值类型而不是参数类型之类的事情。也就是说,编译器在它应该只实例化完全指定的模板时实例化两个模板。不过,这是没有实际意义的猜测,因为该错误已在 gcc >= 4.4 中得到修复。

因为不应该破解 STL_algobase.h ;),Vicente 建议的工作是正确的,即将函数指针转换为所需的函数指针类型 const int& (*)(const int&, const int&) .在您的代码中,强制转换不起作用,因为正如 GMan 指出的那样,您正在强制转换为 boost::function<...>,它对解决函数指针歧义没有任何作用。

关于C++ boost函数重载模板,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2861497/

相关文章:

c++ - 向数学程序测验添加倒数计时器

c++ - 使用 zlib 编译 boost

c++ - 没有默认构造函数的类对象的值初始化

c++ - 使用dynamic_pointer_cast时出现段错误

c++ - 为什么这段代码打印0223?

javascript - 如何用js重定向并在翻到上一页时发出警报?

javascript - angularjs 的两个嵌套点击事件

c++ - 函数参数已在函数声明 C++ 中初始化

c++ - 链接 boost 库时出现另一个 "undefined reference"错误

c++ - Boost.Process - 如何让一个进程运行一个函数?