我試圖用變量初始化一個結構體。但萬一我使用類型名稱=(值...)它只使用最後一個元素進行初始化。它不適合我,但我不知道它的未定義的行爲,編譯器錯誤或其他。我會除了一個錯誤消息或初始化更多的元素。structinit與兩個整數列表被簡化爲一個int
代碼:
struct funct {
funct(int i)
{
std::cout << "init with one\t" << i << std::endl;
}
funct(int i, int j)
{
std::cout << "init with two\t" << i << "\t" << j << std::endl;
}
};
int main() {
funct tempa = funct(42);
funct tempb = 43;
funct tempc = funct(44, 45);
funct tempd = (46, 47); // thats the compiling thing
return 0;
}
輸出:
init with one 42
init with one 43
init with two 44 45
init with one 47
爲什麼會這樣,而不是與2或compileerrormessage初始化?
編譯:G ++(GCC)5.3.0
COMMA OPERATOR。 –
順便說一句,嘗試使用'{}'大括號而不是'()'括號。 http://rextester.com/XDDDLA10902 –