gpt4 book ai didi

c++ - 限制(放大器)功能的默认参数

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:45:03 27 4
gpt4 key购买 nike

以下代码编译失败。错误信息是:

错误一:

error C3930: 'foo' : no overloaded function has restriction specifiers that are compatible with the ambient context ''

错误 2:

error C2660: 'f1' : function does not take 0 arguments

错误 3:

IntelliSense: amp-restricted function "int foo() restrict(amp)" (declared at line 5) must be called from an amp-restricted function

程序:

#include <amp.h>
#include <iostream>
using namespace std;

int foo() restrict(amp) { return 5; }

int f1(int x = foo()) restrict(amp) {
return x;
}

int main()
{
using namespace concurrency;

int a[10] = {0};
array_view<int> av(10, a);

parallel_for_each(av.extent, [=](index<1> i) restrict(amp) {
av[i] = f1();
});

for(unsigned i=0; i<10; ++i) {
cout << av[i] << "\n";
}
return 0;
}

奇怪的是,当我删除 foo() 上的 restrict(amp) 并替换 lambda 中的 f1() 调用时例如,5,程序可以正常编译。那么 amp 函数的默认参数中的函数调用规则是什么?

最佳答案

MSDN Forum answer问题。

The semantics of the default arguments we have chosen are aligned with the overarching premise of C++ that parsing of a program is done in a one left-to-right pass (notwithstanding few significant exceptions to this rule, most notably member functions) - therefore since the restriction specifier is read after the function parameter declaration, any function calls located in default argument expressions are bound according to the "outer" restriction specification, for better or for worse. In other words, you read the program from the beginning with the cpu-restriction "active" (because it's the default one) and switch to restriction X for everything between "restrict(X)" and "}" closing the relevant scope.

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

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