gpt4 book ai didi

c++ - 限制(放大器)功能类型

转载 作者:搜寻专家 更新时间:2023-10-31 00:43:29 25 4
gpt4 key购买 nike

我可以创建 restrict(amp) 函数如下:

auto f = [](int& item) restrict(amp) {item += 1;};

而且我可以在其他restrict(amp) 函数中使用这个函数,例如:

concurrency::parallel_for_each(av.extent,
[=](concurrency::index<1> idx) restrict(amp)
{
f(av[idx]);
}
);

编译后用什么类型代替“auto”?我尝试使用“std::function”:

std::function<void (int&) restrict(amp)> f
= [](int& item) restrict(amp) {item += 1;};

但收到编译错误。

感谢您的关注!

最佳答案

lambda 表达式的结果是一个闭包对象,闭包对象的类型是不可知的。您只能使用auto 来声明其确切类型的变量。

但是,您可以将闭包对象转换std::function 的合适实例,如果 lambda 是非捕获的,您甚至可以将其转换到函数指针。但是,这种转换可能会带来(显着)成本,因此您应该尽可能使用 auto 来处理实际的闭包类型。

bind 表达式也是如此。

相关标准部分为5.1.2(3):

The type of the lambda-expression (which is also the type of the closure object) is a unique, unnamed non-union class type — called the closure type — whose properties are described below. This class type is not an aggregate.

也就是说,我不确定特殊的 AMP 扩展在此上下文中的行为如何,并且可以想象 AMP 限制的 lambda 不能转换为其他任何东西。我会尝试在 AMP 规范中查找它。

更新 AMP Specification 的第 2.2.3 和 2.3 节似乎适用于这个问题。

关于c++ - 限制(放大器)功能类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10480359/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com