【问题标题】:Pass function into function using std::bind使用 std::bind 将函数传递给函数
【发布时间】:2014-10-23 19:25:49
【问题描述】:

过去两天我一直在研究这些功能,在使用 boost 后,我​​的 CPU 和 Wall 时间终于可以工作了,

最后一个我无法解决的问题,我正在尝试将一个带参数的函数传递给另一个函数,该函数使用 std::bind 返回一个值,

我是一名学生,这对我来说是全新的,边走边学,

int CA1::binarySearch(vector<int> v, int target)
{

    int top, bottom, middle;
    top = vecSize - 1;
    bottom = 0;

    while (bottom <= top)
    {
        middle = (top + bottom) / 2;
        if (v[middle] == target)
            return  middle;
        else if (v[middle] > target)
            top = middle - 1;
        else
            bottom = middle + 1;
    }
    return -1;
}


double CA1::measure(std::function<void()> function) {
    auto startCpu = boost::chrono::process_real_cpu_clock::now();
    auto startWall = boost::chrono::process_system_cpu_clock::now();

    function();

    auto durationCpu = boost::chrono::duration_cast<boost::chrono::nanoseconds>
        (boost::chrono::process_real_cpu_clock::now() - startCpu);
    auto durationWall = boost::chrono::duration_cast<boost::chrono::nanoseconds>
        (boost::chrono::process_system_cpu_clock::now() - startWall);


    double cpuTime = static_cast<double>(durationCpu.count()) * 0.000001;
    double wallTime = static_cast<double>(durationWall.count()) * 0.000001;

    /*return static_cast<double>(duration.count()) * 0.000001;*/

    cout << "Cpu time " << cpuTime << endl;
    cout << "Wall time " << wallTime << endl;

    return cpuTime;
}

void CA1::DoTests() {

    auto  time = measure(std::bind(binarySearch, vectorUnordered, 2));
}

我得到的错误是:

error C3867: 'CA1::binarySearch': function call missing argument list; use '&CA1::binarySearch' to create a pointer to member

但从我阅读的内容和其他用户看到的代码 sn-ps 来看,我在 DoTests() 中的代码是正确的。

【问题讨论】:

  • 因为binarySearch 是一个非静态成员函数,你需要像&amp;CA1::binarySearch 这样限定名称,并绑定CA1 的实例,该方法将被调用,例如std::bind(&amp;CA1::binarySearch, this, vectorUnordered, 2)

标签: c++ boost bind std


【解决方案1】:

类函数有明确的'this'参数,需要作为第一个参数传入:

measure(std::bind(&CA1::binarySearch, this, vectorUnordered, 2))

【讨论】:

    【解决方案2】:

    显然,binarySearchCA1 类的成员函数,并且成员函数总是有一个隐含的 this 参数。当您将std::bind 与成员函数一起使用时,您还需要传递this(请参阅“Using std::bind with member function, use object pointer or not for this argument?”),但这不一定是最好的解决方案...

    binarySearch 是否需要成为成员函数? C++ 不会强迫你把所有东西都放在一个类中,你也不应该这样做,除非这样做是有意义的。一般来说,不需要访问类的私有成员的函数不应该是成员函数(参见“How Non-Member Functions Improve Encapsulation”)。此外,标准库已经有std::binary_search

    【讨论】:

    • 是的,我需要访问 binarySearch 中类的私有成员,我已经更改了我的代码,但现在我遇到了一个新错误:请参阅对函数模板实例化 'std::function 的引用::function<:_bind ca1::>>,int ),int,CA1,std::vector<_ty>>,int>,CA1 *const ,std::vector<_ty>> &,int>>( _Fx &&)' 正在编译
    • 我必须为一个作业编写 binarySearch。
    • DoTests 现在的样子如何?
    • void CA1::DoTests() { auto time = measure(std::bind(&CA1::binarySearch, this, vectorUnordered, 2)); }
    • 这似乎是正确的。 this code 和你做的一样吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多