相关疑难解决方法(0)

std :: function的性能开销是多少?

我在论坛上听到使用std::function<>原因性能下降.这是真的吗?如果是真的,这是一个很大的性能下降?

c++ boost std

61
推荐指数
4
解决办法
3万
查看次数

为什么std :: function的初始化程序必须是CopyConstructible?

根据http://en.cppreference.com/w/cpp/utility/functional/function/function,初始化程序的类型,即F形式(5),应满足CopyConstructible的要求.我不太懂.为什么F只是MoveConstructible不行?

c++ templates constructor function c++11

5
推荐指数
1
解决办法
523
查看次数

如何定义一个采用带有 capture 的 lambda 作为参数的函数?

我有这个函数,如果列表true中的任何元素与给定的匹配,则返回predicate

bool _any(bool (*predicate)(MyStructure), list<MyStructure> arr)
{
    for (int i = 0; i < arr.size(); i++)
    {
        if (predicate(arr.front()))
            return true;
        arr.pop_front();
    }
    return false;
}
Run Code Online (Sandbox Code Playgroud)

这适用于非常简单的 lambda,但如果给定的 lambda 需要捕获this,那么我会遇到一个错误,我不知道如何修复。

Assert::IsTrue(_any(
    [this](MyStructure t)
    {
        return t._name == "NAME_SEARCHED" &&
            t._type == "TYPE_SEARCHED" &&
            _any([](OtherStruct o) { return o._name == "SEARCHED_2"; }, t._children);
    },
    myList));
Run Code Online (Sandbox Code Playgroud)

错误:

cannot convert argument 1 from 'UTests::<lambda_e8bda0383e9f0c2ae44be631a7424852>' 
to 'bool (__cdecl *)(MyNameSpace::MyStructure)'
Run Code Online (Sandbox Code Playgroud)

(注意:_any也需要OtherStruct定义)。

c++ algorithm lambda function-pointers c++11

4
推荐指数
1
解决办法
778
查看次数