标签: std-function

Clang ++生成泄漏内存的可执行文件,关于std :: function和lambda

ArchLinux(i686)上的Clang ++ 3.2用于构建以下C++ 11代码

#include <iostream>
#include <functional>

typedef std::function<void ()> Action;
typedef std::function<int ()> Generator;

Action act(Generator const& gen)
{
    return [=]()
    {
        std::cout << gen() << std::endl;
    };
}

int main()
{
    static Generator const gen([]() { return 0; });
    act(gen);
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

随着clang++ test.cpp -std=c++0x && valgrind --leak-check=full --log-file=tmp.log.memcheck ./a.out然后我得到

==600== HEAP SUMMARY:
==600==     in use at exit: 1 bytes in 1 blocks
==600==   total heap usage: 3 allocs, 2 frees, 18 bytes allocated …
Run Code Online (Sandbox Code Playgroud)

c++ memory-leaks clang c++11 std-function

10
推荐指数
1
解决办法
945
查看次数

我应该如何使用默认参数定义std :: function变量?

要将std :: function变量设置为具有默认参数的lambda函数,我可以使用auto如下:

auto foo = [](int x = 10){cout << x << endl;};
foo();
Run Code Online (Sandbox Code Playgroud)

这将打印10.

但我希望foo变量驻留在结构中.在结构中我不能使用auto.

struct Bar
{
    auto foo = [](int x = 10}(cout << x << endl}; //error: non-static data member declared ‘auto’
};
Bar bar;
bar.foo();
Run Code Online (Sandbox Code Playgroud)

auto用std :: function 替换

struct Bar
{
    std::function<void(int x = 10)> foo = [](int x = 10}(cout << x << endl}; //error: default arguments are only permitted for function parameters
};
Bar bar;
bar.foo(); …
Run Code Online (Sandbox Code Playgroud)

lambda c++11 std-function

10
推荐指数
2
解决办法
6646
查看次数

使用std :: function和std :: bind来存储回调并处理对象删除.

我想实现一个管理器,它使用C++ 11将回调存储到多态类的成员函数中.问题是我不知道如何处理成员所属的对象被删除或应该被删除的情况,我想让界面尽可能简单.

所以我想到了以下内容:将a存储std::weak_ptr到对象以及存储std::function到成员.

以下似乎有效:

class MyBase {
public:
    MyBase() {}
    virtual ~MyBase() {}
};
//--------------------------------------------------

class MyClass : public MyBase {
public:
    MyClass() : MyBase() {}
    void myDouble(double val) const { std::cout << "Value is: " << val << std::endl; }
};
//--------------------------------------------------

Class Manager {
public:
    void setFunction(std::weak_ptr<MyBase> base, std::function<void(double)> func) {
        m_function.first  = base;
        m_function.second = func;
    }
private:
    std::pair<std::weak_ptr<MyBase>, std::function<void(double)>> m_function;
};
Run Code Online (Sandbox Code Playgroud)

要使用它:

Manager db;
std::shared_ptr<MyClass> myClass = std::make_shared<MyClass>();
db.setFunction(myClass, std::bind(&MyClass::myDouble, myClass, std::placeholders::_1)); …
Run Code Online (Sandbox Code Playgroud)

c++ stdbind c++11 std-function

10
推荐指数
1
解决办法
1827
查看次数

为什么std :: function可以用一个具有不同返回类型的lambda构造?

以下编译正常:

#include <functional>

int main()
{
    std::function<const int&()> f = []() -> int {return 1;};
    const int& r = f(); // r is a dangling reference
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

为什么可以将返回类型设置为std::function具有const int&返回类型的lambda int?恕我直言,允许这种演员隐含地发生并且没有任何警告.

c++ lambda std-function

10
推荐指数
1
解决办法
512
查看次数

函数模板参数推导(类与功能模板)

你能帮我理解为什么参数推导适用于类模板而不适用于函数模板?

如果我理解正确,类模板定义了一个函数,所以当我调用它时,编译器可以进行隐式转换,但是在函数模板的情况下,目前没有函数定义,所以隐式转换没有发生.

但是我不明白为什么编译器不能创建函数定义然后应用隐式转换?

#include <functional>

template<typename ...ARGS>
class Test1
{
public:
    void add(const std::function<void(ARGS...)>&) {}
};

class Test2
{
public:
    template<typename ...ARGS>
    void add(const std::function<void(ARGS...)>&) {}
};

void func(int) {}

int main()
{
    Test1<int> test1;
    test1.add(func);

    Test2 test2;
    test2.add<int>(func);
}
Run Code Online (Sandbox Code Playgroud)

错误是:

在函数'int main()'中:

   25:24:错误:没有匹配函数来调用'Test2 :: add(void(&)(int))'

   25:24:注意:候选人是:

   14:10:注意:模板void Test2 :: add(const std :: function&)

   14:10:注意:模板参数扣除/替换失败:

   25:24:注意:不匹配的类型'const std :: function'和'void(int)'

c++ templates std-function template-argument-deduction argument-deduction

10
推荐指数
1
解决办法
289
查看次数

std :: bind到包含多种std :: function类型的std :: variant

I'm playing around with callback functions and wish to register multiple functions via std::bind that differ in signatures (altough they all return void). Assigning the result of the std::bind to the std::variant yields in a "conversion to non-scalar type" error. Is it an ambiguity error? Can I provide the compiler with more information?

Dropping the std::bind (which allows the assignment) is not an option as I wish to register the callbacks using some

template <typename Function, typename... Args>
void …
Run Code Online (Sandbox Code Playgroud)

c++ std-function c++17 std-variant

10
推荐指数
2
解决办法
222
查看次数

Lambda 函数使用广义捕获不可能吗?

lambda 可以很容易地转换为 std::function ,尽管当 lambda 使用带有 unique_ptr 的广义捕获时这似乎是不可能的。可能缺少底层 std::move 。是否有解决方法,或者这是一个已知问题?

\n
#include <iostream>\n#include <memory>\n#include <functional>\n\nusing namespace std;\n\nint main()\n{\n    auto lambdaGeneralizedCaptureOk = [t = std::make_unique<int>(1)]()\n      {\n        std::cout << *t << std::endl;\n      };\n    lambdaGeneralizedCaptureOk();\n    \n    // error: use of deleted function \xe2\x80\x98std::unique_ptr<_Tp, _Dp>::unique_ptr(const std::unique_ptr<_Tp, _Dp>&) [with _Tp = int; _Dp = std::default_delete]\xe2\x80\x99\n    std::function<void()> lambdaToFunctionGeneralizedCaptureNok = [t = std::make_unique<int>(2)]()\n      {\n        std::cout << *t << std::endl;\n      };\n    lambdaToFunctionGeneralizedCaptureNok();\n    \n    return 0;\n}\n
Run Code Online (Sandbox Code Playgroud)\n

c++ lambda std-function

10
推荐指数
1
解决办法
462
查看次数

std :: function复制参数?

我的代码:

#include <iostream>
#include <functional>
using namespace std;

struct A {
  A() = default;
  A(const A&) {
    cout << "copied A" << endl;
  }
};

void foo(A a) {}

int main(int argc, const char * argv[]) {
  std::function<void(A)> f = &foo;
  A a;
  f(a);
  return 0;
}
Run Code Online (Sandbox Code Playgroud)

我在控制台上看到"复制A"两次.为什么对象被复制两次而不是一次?我怎样才能正确预防?

c++ c++11 std-function

9
推荐指数
1
解决办法
782
查看次数

使用std :: bind时,为什么复制ctor被调用了两次?

我正在玩,std::functionstd::bind了解如何复制参数,如果我可以保存一些复制操作.

我理解在使用时std::bind,参数是通过值传递而不是引用(除非std::ref指定).但是,当我运行以下代码段时,复制构造函数被调用两次.有人可以解释原因吗?

struct token
{
    static int i;
    int code;

    token()
        : code(i++)
    {
        cout << __FUNCTION__ << ": " << code << endl;
    }
    virtual ~token()
    {
        cout << __FUNCTION__ << endl;
    }

    token (token const & other)
        : code (other.code)
    {
        cout << "copy ctor: " << code << endl;
    }

    // update -- adding a move ctor
    token (token const && other)
        : code (std::move(other.code))
    {
        cout << "move …
Run Code Online (Sandbox Code Playgroud)

c++ stdbind std-function

9
推荐指数
1
解决办法
229
查看次数

关于“lambda []void ()-&gt;void”自定义转换的问题

TestCase2并且TestCase3可以正常编译。但是,TestCase1我收到以下错误:

E0312, Custom conversion from "lambda []void ()->void" to
       "EventHandler" is not appropriate.
Run Code Online (Sandbox Code Playgroud)

为什么我会收到此错误?我想知道如何解决。

#include <functional>
#include <iostream>

class EventHandler
{
    std::function<void()> _func;

public:
    int id;
    static int counter;

    EventHandler() : id{ 0 } {}
    EventHandler(const std::function<void()>& func) : _func{ func }
    {
        id = ++EventHandler::counter;
    }
};

int EventHandler::counter = 0;

int main()
{
    EventHandler TestCase1 = []() {};
    EventHandler TestCase2([]() {});
    EventHandler TestCase3 = static_cast<std::function<void()>>([]() {});
}
Run Code Online (Sandbox Code Playgroud)

c++ lambda class c++11 std-function

9
推荐指数
1
解决办法
343
查看次数