我有一個模板,它爲每個類型創建一個唯一的標識符。這裏有一個精簡版的模板:基於類型創建唯一標識是否有一個很好的模式?
template <typename T>
class arType {
static const arType Id; // this will be unique for every instantiation of arType<>.
}
// Address of Id is used for identification.
#define PA_TYPE_TAG(T) (&arType<T >::Id)
這個工作原理是當你的可執行文件完全由靜態庫構成時。不幸的是,我們正在轉向由dll組成的可執行文件。每個DLL可能有一個類型的Id自己的副本。
一個明顯的解決方案是顯式實例化arType的所有實例。不幸的是,這很麻煩,我想問問有沒有人可以提出更好的解決方案?
你可以看看http://stackoverflow.com/questions/1708458/template-metaprogram-converting-type-to-unique-number – 2010-03-16 20:50:38
一個明顯的問題是「爲什麼你認爲你需要這樣做?「 – 2010-03-16 21:06:43
我希望能夠在運行時存儲唯一標識符並將其用於比較。我們使用它的一個地方是類型之間的轉換表。 – 2010-03-16 21:34:27