问题标签 [function-object]
For questions regarding programming in ECMAScript (JavaScript/JS) and its various dialects/implementations (excluding ActionScript). Note JavaScript is NOT the same as Java! Please include all relevant tags on your question; e.g., [node.js], [jquery], [json], [reactjs], [angular], [ember.js], [vue.js], [typescript], [svelte], etc.
c++ - 返回绑定的本地函数对象会导致未定义的行为吗?
我在下面给出一个例子。该程序编译并运行良好,但我想知道它是否是根据 C++11 标准理论上未定义的行为;我可以返回绑定(临时)本地函数对象的结果吗?
示例代码(简称编辑):
编辑原始示例:
c++ - C++11:将 lambda 转换为源代码中的函数对象
我有一个 C++11 代码库,我需要在不支持 lambda 的旧编译器上构建它。手动将所有 lambdas 更改为手工制作的函数对象是不切实际的。
有谁知道我可以作为预编译步骤运行的工具,它会自动将所有 lambda 提取到它们的等效函数类中?我想知道我是否可以使用clang的前端。
c++ - 自定义排序和映射时更喜欢函数指针还是函数对象?
我想在 C++ 中自定义排序模板和地图模板
这里是为了比较,
经过我的测试,Greater1 适用于地图,Greater2 适用于排序。我还从 CPLUSPLUS 中获得了一些信息,发现和 map 都应该使用函数指针和函数对象。我的问题是为什么 Greater2 可以用于 map 而 Greater1 可以用于排序。
c++ - cout 以相反的顺序打印
在以下示例中尝试重载 operator():
它以相反的顺序将 fib 序列打印为 8、5、3、2、1、1。我知道状态保持在 () 重叠但为什么打印以相反的顺序显示?
c++ - 关于C++中函数对象的问题
我对以下代码有疑问。
调用的时候写成:
不是很明白,因为operator()需要一个参数“元素”,但是调用的时候没有包含。为什么?
IsMultiple 的示例在调用时实际上给出了一个参数。为什么这两个不一样??
matlab - Matlab 是否支持函数对象?
试图弄清楚我是否可以访问在我们的 Matlab 脚本中使用的函数对象编程技术。这类似于.NET 的 Func 类型或Python 的函数对象。Matlab 是否给函数提供一流的对象状态?
c++ - 使用未绑定的函数对象实例化函数对象会导致左值错误
我有以下函数对象:
当我尝试像这样实例化 Wrapper 对象时:
我明白了
“错误:没有匹配的构造函数用于初始化'const DOTf'(又名'const DOTWrapper')const DOTf dotf((NConv()),(LConv()));”
这是为什么?NConv 和 LConv 毕竟在内存中占有一席之地......所以......它们不应该是左值吗?
c++ - Function object conversion to function pointer
I am looking for a way to convert function object to function pointer. Captureless lambda has implicit conversion that allows to:
I try to do the same with old-fashioned, empty class function objects like:
Or std predicates like std::less<int>
.
One way I found is to wrap call of foo
with lambda.
If I can assure that foo
is stateless and const
I dont really need this ptr and lambda-capture:
But here I do not know how to provide perfect forwarding,
cause [](Args&&...)
or [](auto&&...)
cannot convert to R(*)(Args...)
.
Such trick fails when args is noncopyable like std::unique_ptr<int>
.
I know that I could use std::function
, but it's kind of heavy-weight, while I am trying to get a light-weight solution.
Any advice appreciated.
javascript - 使用 new Function() 将字符串转换为对象 - 这是如何工作的?
我无法让 JSON.parse 将字符串转换为对象,我发现这段代码可以解决我的问题 - 但是,我无法弄清楚它是如何工作的。如果有人可以(向 JavaScript 初学者)解释第 3 行发生了什么,我将不胜感激。谢谢你。
c++ - Function object as template parameter
That's an example from Essential c++. Comp
is obviously a function object class name. Why could I use the Comp(val, _val)
directly? Normally I think I should firstly define a function object like this: Comp comp
, then invoke comp
not Comp
.