- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
不要误会我的意思:Boost 的 bind()
很棒。
但我确实讨厌用它来编写和阅读代码,并且我已经放弃了希望我的同事永远理解/使用它的希望。
我最终得到这样的代码:
btn.clicked.connect(bind(&BetBar::placeBet, this, bet_id));
animator.eachFrame.connect(bind(&Widget::move, buttons[bet_id]));
虽然合乎逻辑,但与我所说的好代码相去甚远。
为了演示...在 C++1x 中,我们将使用:
btn.clicked.connect([&](int bet_id){ placeBet(bet_id); })
animator.eachFrame.connect([&](Point newPos) { buttons[bet_id].move(newPos) })
一个好的 DSL 可能看起来像这样:
on(btn.clicked) placeBet(bet_id);
on(animator.eachFrame) buttons[bet_id].move(eachFrame::newPos);
您如何处理 C++ 中的绑定(bind)?你只是接受给你带来的 boost 吗?
最佳答案
您似乎想要以下内容:
this
第一个在今天的 C++ 中非常困难,因为 this
在极少数情况下是隐式的,你当然不能将它隐式地传递给函数。 IE。你不能用库函数来实现这个,但你可以用宏来实现。尽管如此,它还是很丑陋。
第二部分要容易得多:
button.clicked.handler = bind(BetBar::placeBet, this, bet_id);
这只需要 handler.operator=(boost::function<void(*)()> const&)
第三个又难了,因为你刚刚又设计了一个两阶段名称查找的案例。这对模板来说已经够难的了。 boost 的 _1 技巧通过明确指出以后应绑定(bind)哪些参数来起作用。但是,_1 作为名称并不神奇。它基本上是一个返回 boost::arg<1> 的免费函数。因此,通过对 animator.eachFrame.newPos 的适当定义,可以使以下内容等效:
animator.eachFrame.handler = bind(&Widget::move, buttons[bet_id], _1)
animator.eachFrame.handler = bind(&Widget::move, buttons[bet_id], animator.eachFrame.newPos)
关于c++ - 更自然的 boost::bind 替代方案?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/500819/
我有一套使用两种语言的文档:英语和德语。关于这些文档没有可用的元信息,程序只能查看其内容。基于此,程序必须决定用哪种语言编写文档。 是否有可以在几个小时内实现的针对该问题的“标准”算法?或者,一个免费
背景 我有一个日志系统,可以将记录输出到 std::ostream .每条记录都用一个计数器进行注释,该计数器随着每个输出而增加 1,如下所示: ===== Batch # 5 ===== T
用户可能希望根据需要分隔数字。 从字符串中提取所有(自然)数字的最有效(或简单的标准函数)是什么? 最佳答案 您可以使用正则表达式。我从 Sun's regex matcher tutorial 修改
我认为如果表有代理键而没有(自然)替代键是没有意义的(请记住,代理键的属性之一是它在数据库之外没有意义环境)。 例如假设我有下表: 假设 employee_id 是代理主键,表中没有(自然)备用键。
我想将屏幕方向锁定为其默认方向。我在实现这一点时遇到问题。最初我将屏幕锁定为 list 中的肖像。它适用于纵向默认设备。但是许多平板电脑默认为横向,因此在这些设备中锁定纵向是不合适的,我想检测此默认方
我已将笔记本电脑上的触摸板滚动设置为倒置(自然)。它适用于任何地方(pdf、浏览器等),但在 vscode 中,它坚持正常滚动。通过 vscode 的设置文件没有显示适当的条目。 系统:Ubuntu
在我发现的许多在上限集合上使用可尾游标的示例中,代码包括: hint( { $natural: 1 } ) (例如 here ),包括官方文档 ( here ),以“确保我们不使用任何索引”,并且结果
@override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( title: T
一些上下文:Node.js、Bot、natural module . 我想构建一个机器人,并且我正在使用自然模块来解析用户输入并对其进行总体分类。 var classifier = new natur
我是一名优秀的程序员,十分优秀!