2

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!

Benj
  • 31,668
  • 17
  • 78
  • 127
synacker
  • 1,722
  • 13
  • 32
  • There seems to be a problem with the function type in `std::function`'s template argument. Did you try: `std::function f = ...`? – dirkgently May 07 '12 at 10:23
  • It does not work. Highlights intellisense error. – synacker May 07 '12 at 10:28
  • 1
    I'm not familiar with MSVC, but I've heard before that the compiler, the compiler frontend and the IDE/Intellisense are all separate, not-quite-compatible products, and so it's possible that one complains about something that the other accepts. Caveat emptor. – Kerrek SB May 07 '12 at 10:31
  • Yes, i agree, but also exist compilation error. – synacker May 07 '12 at 10:35
  • Can you try just `std::function`, or `void(*)(int&)`? – Kerrek SB May 07 '12 at 10:55

1 Answers1

5

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.

Kerrek SB
  • 464,522
  • 92
  • 875
  • 1,084
  • @Milovidov: I see, sorry for that. I'd have to consult the AMP specification (which is freely available, by the way). – Kerrek SB May 07 '12 at 10:35