I am writing a library using C++AMP for the internals, and I have discovered the following does not work and I am curious as to why (as it works if I remove restrict(amp)
from the functor):
template <typename T>
using SumType = decltype( std::declval<T>() + std::declval<T>() );
template <typename T, typename Func>
auto TestFunc(T t, Func f) -> SumType<decltype(f(t))>
{
return f(t) + f(t);
}
int main() {
auto f = []( float flVal ) restrict(amp) {
return flVal * flVal;
};
float flResult = TestFunc( 1.0f, f ); // Error here: "no instance of function template 'TestFunc' matches the argument list"
}
However, if I remove the restrict(amp)
from the definition of f
, the program compiles as expected. Is there a clause in the AMP open-standard that can shed light on this?
Moreover, if we use the following in the definition of TestFunc
, it compiles correctly even with the restrict(amp)
specifier:
template <typename T, typename Func>
auto TestFunc( T t, Func f ) -> T
{
return f(t) + f(t);
}
I believe what you want is section 2.2 of the AMP Specification. You are getting the error "no instance of function template 'TestFunc' matches the argument list" because your code which calls TestFunc
is not inside and restrict(amp) lambda (as part of a parallel_for_each
. The compiler is therefore looking for a TestFunc with a restrict(cpu)
decoration on it.
Does your code compile if you change your declaration as follows:
auto f = [](float flVal) restrict(amp, cpu) {
return flVal * flVal;
};
Calling an amp restricted function/lambda outside the context of an AMP parallel_for_each
is somewhat meaningless and will never work. Even if you can trick the compiler into not erroring.