2014-03-05 45 views
2

如何將默認值賦予std ::數組?例如,像如何使用Visual C++ 2012將默認值賦給std :: array?

void f(std::array<int, 3> pt = std::array<int, 3>{0, 1, 2}); 

在檢查與有用的評論。我認爲這是由於編譯器。如何在VS 2012中處理它而不創建像std::array<int, 3> MakeArray(...)這樣的函數?

+3

你嘗試了嗎?提示:它的作品。 –

+0

就是這樣。它不適合你嗎? –

+1

你問過你的編譯器嗎?因爲我問了我的問題,他對此很好。 – Borgleader

回答

3

請嘗試以下

void f(std::array<int, 3> pt = {0, 1, 2}); 

或者我會寫簡單的

void f(std::array<int, 3> = {0, 1, 2}); 

在GCC這個代碼不編譯。它似乎是編譯器的一個bug。然而,你可以寫GCC無論是作爲

void f(std::array<int, 3> = std::array<int, 3>({ 1, 2, 3 })); 

void f(std::array<int, 3> = { { 1, 2, 3 } }); 
+0

這對GCC 4.8來說不起作用(調用'f()'會給出一個錯誤:'數組必須用一個大括號初始化符來初始化)。 OP的版本確實有效。 – interjay

+0

@interjay,它在Clang上工作,但GCC希望雙花括號。 – chris

+0

@interjay查看我更新的帖子。 –