[問題] typeinfo about const

看板C_and_CPP (C/C++)作者 (小乖)時間16年前 (2009/12/19 17:26), 編輯推噓1(105)
留言6則, 3人參與, 最新討論串1/1
我玩了一下 typeinfo 的功能, 想比較一下 int 型別 與 const int 型別 結果出乎我意料的 typeid(int) == type(const int) 回傳的結果是 true @@" 我稍微 google 了一下,找不到關於 typeinfo 與 const 的關西 讓我陷入了苦惱 #(┬_┬)# 請教各位高手有什麼看法呢? 謝謝 我目前用的是 Visual Studio 2005 以下是我的 test code #include <iostream> #include <typeinfo> using namespace std; int main() { int ia; cout << "ia is: " << typeid(ia).name() << endl; const int cia = 0; cout << "cia is: " << typeid(cia).name() << endl; if(typeid(ia) == typeid(cia)) cout << " ia == cia"; else cout << " ia != cia"; cout << endl; } -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 114.43.16.28

12/19 19:11, , 1F
打typeid第二篇就有
12/19 19:11, 1F

12/19 19:45, , 2F
The typeid operator ignores top-level const 多謝
12/19 19:45, 2F

12/19 19:46, , 3F
試試 type trait 吧
12/19 19:46, 3F

12/19 21:25, , 4F
type_trait 似乎只能測型別不能測變數的型別
12/19 21:25, 4F

12/19 21:26, , 5F
我用的是 boost::is_same<int,const int>
12/19 21:26, 5F

12/19 21:27, , 6F
請問 boost 是否有類似 is_same(ia,cia) 之類的 XD
12/19 21:27, 6F
文章代碼(AID): #1BB9nZMe (C_and_CPP)
文章代碼(AID): #1BB9nZMe (C_and_CPP)