顯示具有 typeid 標籤的文章。 顯示所有文章
顯示具有 typeid 標籤的文章。 顯示所有文章

2012年8月17日 星期五

typeid in c++11

在 C++11 裡面把 typeid 拔掉, 取而代之的是 decltype

所以如果 code 有到的話

目前過渡期解法就在 Compile Flag 加個 `-Dtypeof=decltype` 吧

範例:
g++ -std=c++0x -Dtypeof=decltype foo.cpp
g++ -std=c++11 -Dtypeof=decltype foo.cpp



但如果 compiler 跟你抱怨 rvalue 不能轉 T& 的話...就用下面這有點邪門的解法吧


首先先在 code 頂端或著某個一定會 include 的 header 檔加入以下的 code
template<class T> struct unref     { typedef T type; };
template<class T> struct unref<T&> { typedef T type; };

然後編譯選項換成下面這樣
g++ -std=c++0x -Dtypeof\(x\)="unref<decltype(x)>::type" foo.cpp
g++ -std=c++11 -Dtypeof\(x\)="unref<decltype(x)>::type" foo.cpp

c++ 真的是越來越難學易用了XD...

參考

How can I use decltype to get the type of a reference? (from Stackoverflow)