很多函数还是很多 Lambda?

发布于 2024-10-11 04:15:54 字数 1009 浏览 4 评论 0原文

在此之前我已经发布了一些关于在快速委托上使用 std::function 的问题,以及如何将 std::function 存储在集合来展示可以添加和删除的事件的行为。我还询问了编写大量 EventArg 类型的小类时的最佳实践,并且这个小小的设计决策也已被搁置。这是一个很棒的社区!

现在,除了序言之外,我的结构已经就位,是时候编写所有处理传入数据的处理程序了。我有一个 std::map,看起来像这样:

typedef std::function<void(const CommandData&)> CommandDelegate;
typedef boost::shared_ptr<CommandDelegate> CommandDelegatePtr;
typedef std::map<short, CommandDelegatePtr> CommandMap;

我希望向其中添加大约 200 个处理程序。我可以在标准成员函数和 lambda 之间进行选择。

在考虑成员函数时,我首先想到的是 200 个声明和 200 个实现以及一个大源文件。

我没有用所有这些处理程序污染我的类,而是想“好吧,它们只是句柄,为什么不使用 lambda?这看起来很简单,当构造类时,它可以将所有这些匿名函数分配给映射。工作完成!

然后 我可以调用它自己的文件。

我意识到

  1. 构造函数会很大。由于.h 文件中的 200 个声明, , .cpp 文件中的 200 个实现(以及其他函数)
  2. .h 文件中的 200 个声明,一个单独的“handlers.cpp` 实现文件
  3. 无声明,在 ctor 中分​​配了 200 个 lambda
  4. 无声明,200 的文件中的 initializeMap 函数中分配。

lambda 在其自己

I've posted a few questions prior to this one regarding the use of std::function over fast delegates, and ways in which one can store std::function in a collection to exhibit the behavior of an event that can be added and removed to. I also enquired as to best-practice when writing a whole ton of little EventArg-type classes, and that little design decision has also been put to rest. This is a great community!

Now, preamble aside, I have must of my structure in place and it is time to write all the handlers that will deal with incoming data. I have a std::map, that looks like this:

typedef std::function<void(const CommandData&)> CommandDelegate;
typedef boost::shared_ptr<CommandDelegate> CommandDelegatePtr;
typedef std::map<short, CommandDelegatePtr> CommandMap;

And I wish to add about 200 handlers to this. I have the choice between standard member functions, and lambdas.

The first thing I thought about, when thinking about member functions, was 200 declarations and 200 implementations and one big-ass source file.

Rather than pollute my class with all these handlers I thought "Well, they're just handles, why not use lambdas? It seems simple enough, when the class is constructed it can assign all these anonymous functions to the map. Job done!

Then I realized the constructor would be huge. I could call a 'initializeMap` helper function that could conceivably go in it's own file due to the size.

What do you guys think?

  1. 200 declarations in the .h file, 200 implementations (amongst other functions) in the .cpp file
  2. 200 declarations in the .h file, a separate 'handlers.cpp` implementation file
  3. No declarations, 200 lambdas assigned in the ctor
  4. No declarations, 200 lambdas assigned in an initializeMap function in it's own file.

Thanks in advance!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

地狱即天堂 2024-10-18 04:15:54

我的观点是,尽可能使用 lambda。它们更易于维护。例如,如果您有一个成员函数,则每次更改它时都必须更新声明和定义,并且还必须为其分配唯一的名称。 Lambda 是更好的选择。如果我可以对成员变量进行自动类型推导,我就永远不会使用成员函数。

My opinion is, use lambdas wherever possible. They're much more maintainable. For example, if you have a member function, you will have to update the declaration and definition every time you change it, and you will also have to assign it a unique name. Lambdas are the superior option. If I could have automatic type deduction on member variables, I wouldn't ever use member functions.

淡写薰衣草的香 2024-10-18 04:15:54

您真的需要这些函数是动态的吗?因为如果您唯一关心的是不污染您的主类,那么有更好(更快)的解决方案,例如创建子类,或者只是将所有代码分成多个文件。

如果您的标头包含 200 个函数,但从不更改,那么它不会使您的项目变得如此臃肿,因为它只是躺在那里。另一方面,臃肿的构造函数更糟糕,因为您很有可能有时必须更改它,然后它必须重新编译所有这 200 个初始化。

无论如何,编译时间可能不会那么长,但为什么要这么麻烦呢?

我只是将它们的函数与声明一起保留在您的主类中,或者在其他一些专用类或文件中,但不会在 ctor 中动态初始化它们。

Do you really need those functions to be dynamic? Because if your only concern is to not pollute your main class, there are better (faster) solutions, like making a subclass, or just dividing all the code into multiple files.

If you have your header with the 200 functions, but in never changes, it won't bloat your project so much, because it will just lie there. A bloated constructor on the other hand is worse, because there is a higher chance that you will have to change it sometime, and then it will have to recompile all those 200 initialisations.

That compile time probably won't be that long anyways, but why bother at all?

I would just keep them functions with declarations, either in your main class, or in some other dedicated class or file, but not initalising them dynamically in ctor.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文