2012-11-30 103 views
5

以下代碼無法編譯。錯誤消息是:限制(amp)功能的默認參數

錯誤1:

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; 
} 

奇怪的是,當我刪除restrict(amp)foo(),並用5代替lambda中f1()的調用時,程序編譯得很好。那麼函數調用的默認參數的規則是什麼?

+0

我遇到了來自GCC和MSVC的奇怪消息。事實證明,我有一個'MyStruct'數組,並且元素的數量被關閉了一個:'MyStruct s_val [3] = {MyStruct(...),MyStruct(...)};'。編譯器都沒有告訴我計數已關閉。 – jww

回答

2

MSDN Forum answer這個問題。

的我們選擇默認參數的語義與C++的程序在一個左到右通做的是分析的首要前提(排列儘管一些顯著的例外,最值得注意的是成員函數) - 因此,由於限制說明符是在函數參數聲明之後讀取的,所以根據「外部」限制規範來限制位於默認參數表達式中的任何函數調用,無論是好還是壞。換句話說,你從cpu-restriction「active」開始讀取程序(因爲它是默認的),並且在關閉相關範圍的「restrict(X)」和「}」之間的所有內容切換到限制X.

+0

你可能應該解釋一下。它不清楚你在什麼(除了引用MSDN)。 – jww