[問題] sizeof 測量暫時物件遇到的問題
考慮下列程式
void test_sizeof()
{
struct A{};
sizeof(A());
}
//in VC 2008 & 2010
//error C2066: cast to function type is illegal
//in GCC 4.5
//warning: invalid application of 'sizeof' to a function type
由錯誤訊息來看,compiler 把 sizeof(A()) 當成 function type
於是我改寫這樣的 code
void test_sizeof()
{
struct A(){ public: A(int a=0){} };
sizeof(A(0));
}
compiler 可以順利通過。
我在網路上找了一些資料,有個 hack 方式可以讓第一種方式順利編過
http://erdani.com/errata/display.html?book_index=1&printing=13&book_name=Modern%20C++%20Design
void test_sizeof()
{
struct A{};
sizeof((A())); // 多加括號
}
我的問題是,為什麼多加了這一對括號就可以讓 VC compiler Happy 呢 XD
謝謝!
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 220.134.96.34
推
12/06 12:59, , 1F
12/06 12:59, 1F
推
12/06 13:00, , 2F
12/06 13:00, 2F
※ 編輯: spider391 來自: 220.134.96.34 (12/06 13:40)
→
12/06 13:40, , 3F
12/06 13:40, 3F
推
12/06 15:22, , 4F
12/06 15:22, 4F
→
12/06 15:23, , 5F
12/06 15:23, 5F
→
12/06 15:24, , 6F
12/06 15:24, 6F
→
12/06 15:25, , 7F
12/06 15:25, 7F
推
12/06 17:23, , 8F
12/06 17:23, 8F
→
12/06 17:24, , 9F
12/06 17:24, 9F
→
12/06 17:24, , 10F
12/06 17:24, 10F
→
12/06 17:44, , 11F
12/06 17:44, 11F
→
12/07 01:07, , 12F
12/07 01:07, 12F
→
12/07 01:08, , 13F
12/07 01:08, 13F
→
12/07 14:37, , 14F
12/07 14:37, 14F
→
12/07 14:38, , 15F
12/07 14:38, 15F