【问题标题】:boost::function and plain function pointers: ambigous overloadboost::function 和普通函数指针:模棱两可的重载
【发布时间】:2010-07-09 00:57:09
【问题描述】:

给定以下成员函数重载以采用各种仿函数

class Foo { 
public: 
     void bar(boost::function<void(int)> func); 
     void bar(boost::function<void(float)> func); 
     void bar(boost::function<void(const std::vector<float>&)> func); 
}

和功能

void baz(float f) { std::cout << "float :" << f << std::endl; }

那为什么要取baz的普通函数指针

Foo foo; 
foo.bar(&baz);

产生此错误:

error: call of overloaded ‘bar(void (*)(float))’ is ambiguous 
note: candidates are: void Foo::bar(boost::function<void(int)>) 
note: void Foo::bar(boost::function<void(float)>) 
note: void Foo::bar(boost::function<void(const std::vector<float, std::allocator<float> >&)>)

如何解决这种歧义?

【问题讨论】:

    标签: function-pointers overloading functor boost-function


    【解决方案1】:

    不漂亮也不安全:

    foo.bar( static_cast<function<void(float)> >( &baz ) );
    

    【讨论】:

      【解决方案2】:

      另一种选择:使用Initializers

      foo.bar( function<void(float)>{ &baz } );
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-07-23
        • 2010-12-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-12-27
        相关资源
        最近更新 更多