- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
在某些语言中,调用函数时可以省略括号。
frobnicate foo, bar, baz, qux
让我们看看是否可以将它或类似的东西引入 C++。这几乎肯定意味着滥用逗号运算符,但据我所知,没有办法扩展任意类型以具有重载的逗号运算符。所以像代理 currier 类型的东西可以工作。
comma_curry(frobnicate), foo, bar, baz, qux;
comma_curry
(我不擅长起名字)可能是某种递增的 currier,其逗号运算符一次接受一个参数。理想情况下,我希望它具有完美的转发语义,这样它与直接调用 frobnicate
没有什么不同。
frobnicate(foo, bar, baz, qux);
特别是如果一些参数本身是函数调用,可能会返回纯右值。
frobnicate(foo(), bar, baz(), qux);
我试过并想出了 a non-working example ,使用从另一篇文章复制的一些帮助代码。任何人有一些好的或更好的想法? 👀
最佳答案
当然,所有这些对于普通函数参数来说都是微不足道的。
除非用于学习和玩具示例,否则我不建议使用逗号运算符。不要将其放入生产代码中。
但是使用逗号运算符对于增量和重新柯里化(Currying)也很有趣。这是一个工作示例:
template<typename F, typename... Curry>
struct comma_curry {
template<typename... C>
explicit comma_curry(F function, C&&... c) noexcept :
function(std::move(function)), curry{std::forward<C>(c)...} {}
template<typename T>
friend auto operator,(comma_curry&& self, T&& arg) -> comma_curry<F, Curry..., std::decay_t<T>> {
return std::apply([&](auto&&... curry) {
return comma_curry<F, Curry..., std::decay_t<T>>(std::move(self.function), std::forward<decltype(curry)>(curry)..., std::forward<T>(arg));
}, std::move(self.curry));
}
template<typename... Args>
auto operator()(Args&&... args) const& -> decltype(auto) {
return std::apply([&](auto&&... curry) -> decltype(auto) {
return function(std::forward<decltype(curry)>(curry)..., std::forward<Args>(args)...);
}, curry);
}
template<typename... Args>
auto operator()(Args&&... args) && -> decltype(auto) {
return std::apply([&](auto&&... curry) -> decltype(auto) {
return std::move(function)(std::forward<decltype(curry)>(curry)..., std::forward<Args>(args)...);
}, std::move(curry));
}
private:
// [[no_unique_address]]
F function;
// [[no_unique_address]]
std::tuple<Curry...> curry;
};
它支持通过 std::ref
对引用进行柯里化(Currying),并且仅支持对移动类型进行柯里化(Currying)。
可以这样使用:
int main() {
auto function = [](int& i, double, std::unique_ptr<int>, std::tuple<int>) {
std::cout << "Called! i value: " << i << std::endl;
};
int number = 1;
// Reference arguments are sent using std::ref
auto curried = (comma_curry(function), std::ref(number), 1.5);
curried(std::make_unique<int>(), std::tuple{1});
number = 42;
auto recurried = (std::move(curried), std::make_unique<int>(), std::tuple{1});
// We curried a std::unique_ptr, our function is a one time call
// Since it's a one time call and destroy itself in the calling,
// it must be moved
std::move(recurried)();
}
关于c++ - 滥用逗号运算符来柯里化(Currying)函数参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58958367/
NSNotificationCenter 真的很酷,我们可以用它构建非常可扩展的应用程序。我的问题是:我们是否可以在应用程序中滥用 NSNotificationCenter,即密集使用它,还是它不是一
据我了解,标签是“ anchor ”标签,它有三个用途: 1) 描述并命名页面上的项目 2) 单击时将用户转到另一个页面 Yahoo! 3) 单击时将用户发送到同一页面中的另一个位置 Foo! 有人
我有一个使用 OpenMP 并行化 for 循环的程序。在循环内部,线程将写入共享变量,因此我需要同步它们。但是,我有时会遇到段错误或双重释放或损坏错误。任何人都知道会发生什么?感谢致敬!这是代码:
我很确定过去曾有开发人员严重滥用了我们可怜的 ASP.NET session 状态。 Session 很想帮助跟踪请求之间的事情,但只能做这么多!请帮我减轻它的痛苦,让它和我们一起快乐地工作。 有谁知
关闭。这个问题是opinion-based .它目前不接受答案。 想改进这个问题?更新问题,以便 editing this post 可以用事实和引用来回答它. 4年前关闭。 Improve this
Git 本质上是事件存储的实现,其中存储的数据是目录结构中的文件。已知可以可靠地解决问题: 存储更改历史记录 向客户端传输最少的数据以获取最新数据 可以回滚到之前的状态 可以通过在 Git 上编写包装
我有两项服务-Lincr和LinkBunch。 Lincr是简单的简捷URL缩短服务,而LinkBunch可让您将多个链接缩短为一个链接。我在服务中张贴了太多垃圾邮件,因此不得不关闭Lincr。现在,
我必须接管一个包含多个层、服务和组件的巨型整体。 当我浏览代码时,我首先意识到的是 MDC 的使用. 一些例子: public void setContextOrderId(String orderI
我正在尝试制作一种从外部文件中提取电子邮件信息并将其存储在字符串中的方法。我正在使用 .get 来提取字符,但我的调试器说:没有重载函数的实例与参数列表匹配。它还说它不能将第三个参数从 const c
我的 UI 中需要一个标签来容纳最多 32 个字符而不变形,因此我启用了自动收缩并将行数更改为 0: 这是我在 IB 上的标签: 现在,当我运行应用程序时,它会在屏幕外运行: 我怎样才能使它相应地适应
我有一个整数列表(当前),我想检查这个列表是否包含预期列表中的所有元素,甚至不包含列表 notExpected 中的一个元素,所以代码如下: List expected= new ArrayL
目前正在阅读 Bloch 的Effective Java(第 2 版),他以粗体字指出,在 Web 应用程序中过度使用 POST 本质上是不好的。不幸的是,他没有具体说明原因。 这让我大吃一惊,因为当
提前为第一个可能很愚蠢的帖子道歉。虽然有很多关于这个主题的 Material ,但其中很少有是确定的和/或对我来说可以理解的。 我有一个 AlignedArray 模板类,可以在堆上以任意对齐方式动态
MSDN 建议 RegisterWindowMessage() 函数仅用于注册要在进程之间发送的消息。如果需要在一个进程内发送消息,可以从 WM_APP 到 0xBFFF 范围内安全地选择它。 但是在
我已经阅读了很多关于在每个 SELECT 上使用许多 JOIN 语句的关系数据库。但是,我一直想知道滥用这种方法从长远来看是否会出现任何性能问题。 例如,假设我们有一个users 表。我通常会添加“最
所以我有一些使用保留字 property 的遗留代码,嗯,错了。在继承的基类中,它们基本上已实现。 class TestClass(object): def __init__(self, pr
出于方便和安全的原因,我想使用 using 语句从池中分配和释放对象 public class Resource : IDisposable { public void Dispose()
您很可能已经知道,在 JQuery 中选择文档中具有特定 CSS 类的所有元素,然后使用链接将公共(public)事件处理程序分配给所选元素很简单: $(".toolWindow").click(to
我需要解析一个 xml 文件,它实际上是一个非常大的树结构的图像,所以我使用 XmlReader 类来“动态”填充树。每个节点仅通过 ReadSubtree() 函数传递它期望从其父节点获得的 xml
我想知道这些指南的用途: 1 - 我多久可以读取 NSUserDefaults 2 - 我可以在 NSUserDefaults 中合理存储多少数据 显然,NSUserDefaults 的使用量是有限制
我是一名优秀的程序员,十分优秀!