I can create restrict(amp) function as follows:

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

And I can use this function in other restrict(amp) functions, for example:

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

What type of substituted instead "auto" after compilation? I tried to use the "std::function":

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

but received a compile error.

Thank you for your attention!

有帮助吗?

解决方案

The result of a lambda expression is a closure object, and the type of the closure object is unknowable. You can only use auto to declare a variable of its exact type.

However, you can convert a closure object into a suitable instance of an std::function, and if the lambda is non-capturing, you can even convert it to a function pointer. However, this conversion may come at a (significant) cost, so you should prefer using auto as much as possible to handle the actual closure type.

The same goes for bind expressions.

The relevant standard section is 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.

That said, I'm not sure how the special AMP extensions behave in this context, and it's conceivable that AMP-restricted lambdas are not convertible to anything else. I'll try and look this up in the AMP specification.

Update: Sections 2.2.3 and 2.3 of the AMP Specification seem to apply to this question.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top