我应该复制一个std ::函数或者我可以总是参考它吗? [英] Should I copy an std::function or can I always take a reference to it?

查看:238
本文介绍了我应该复制一个std ::函数或者我可以总是参考它吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的C ++应用程序(使用Visual Studio 2010)中,我需要存储std ::函数,如下所示:

  class MyClass 
{
public:
typedef std :: function< int(int)>我的功能;
MyClass(Myfunction& myFunction);
private:
MyFunction m_myFunction; //我应该使用这个吗?
MyFunction& m_myFunction; //或者我应该使用这个?
};

如你所见,我在构造函数中添加了函数参数作为参考。



但是,在我的类中存储函数的最佳方法是什么?




  • 存储函数作为参考,因为std :: function只是一个函数指针,函数的'可执行代码'保证保留在内存中?

  • 我必须做一个



我的直觉感到说它可以安全地存储一个引用-参考)。我期望编译器在编译时生成lambda的代码,并在应用程序运行时将此可执行代码保存在虚拟内存中。因此,可执行代码从不删除,我可以安全地存储对它的引用。但这是真的吗?

解决方案


我可以存储函数作为参考,因为std ::函数只是一个函数指针,函数的'可执行代码'保证保留在内存中。


std :: function 不仅仅是一个函数指针。它是一个任意可调用对象的包装器,并且管理用于存储该对象的内存。与任何其他类型一样,只有当您使用其他方式保证引用对象在使用该引用时仍然有效,才可以安全地存储引用。



除非你有一个很好的理由来存储一个引用,并保证它保持有效的方式,存储它的值。



通过 const 引用构造函数是安全的,并且可能比传递值更有效。传递非 - const 引用是一个坏主意,因为它阻止你传递一个临时,所以用户不能直接传递一个lambda,结果 bind ,或除 std :: function< int(int)> 本身之外的任何其他可调用对象。 $ b

In my C++ application (using Visual Studio 2010), I need to store an std::function, like this:

class MyClass
   {
   public:
      typedef std::function<int(int)> MyFunction;
      MyClass (Myfunction &myFunction);
   private:
      MyFunction m_myFunction;    // Should I use this one?
      MyFunction &m_myFunction;   // Or should I use this one?
   };

As you can see, I added the function argument as a reference in the constructor.

But, what is the best way to store the function in my class?

  • Can I store the function as a reference since std::function is just a function-pointer and the 'executable code' of the function is guaranteed to stay in memory?
  • Do I have to make a copy in case a lambda is passed and the caller returns?

My gut feeling says that it's safe to store a reference (even a const-reference). I expect the compiler to generate code for the lambda at compile time, and keep this executable code in 'virtual' memory while the application is running. Therefore the executable code is never 'deleted' and I can safely store a reference to it. But is this really true?

解决方案

Can I store the function as a reference since std::function is just a function-pointer and the 'executable code' of the function is guaranteed to stay in memory?

std::function is very much not just a function pointer. It's a wrapper around an arbitrary callable object, and manages the memory used to store that object. As with any other type, it's safe to store a reference only if you have some other way to guarantee that the referred object is still valid whenever that reference is used.

Unless you have a good reason for storing a reference, and a way to guarantee that it remains valid, store it by value.

Passing by const reference to the constructor is safe, and probably more efficient than passing a value. Passing by non-const reference is a bad idea, since it prevents you from passing a temporary, so the user can't directly pass a lambda, the result of bind, or any other callable object except std::function<int(int)> itself.

这篇关于我应该复制一个std ::函数或者我可以总是参考它吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆