[問題] function pointer中使用template未定型別

看板C_and_CPP作者 (Zcode)時間11年前 (2013/12/09 14:13), 編輯推噓1(105)
留言6則, 2人參與, 最新討論串1/1
問題(Question): 各位好,請問大家 function pointer中,若想要用template變數的話(型別未定),要怎麼樣使用呢? 例如我先定義FunctionTypeA,代表的是 輸出為void、輸入為一個int參數 的函數型別 typedef void (*FunctionTypeA) (int& anItem); 這樣的話我往後可以把這種型別的函數餵給其他函數去呼叫 例如void function(FunctionTypeA func) { ... } 但是今天我FunctionTypeA的的某些型別未定,例如上面的參數int,不確定要用int 我想要把int換成template <class T>中的T 如果 typedef void (*FunctionTypeA) (T& anItem); 這樣去寫 g++會出現 [Error] typedef 'FunctionTypeA' is initialized (use decltype instead) [Error] 'T' was not declared in this scope [Error] 'anItem' was not declared in this scope 的錯誤 如果是 template <class T> typedef void (*FunctionTypeA) (T& anItem); g++會出現 [Error] template declaration of 'typedef' 請問大家知道要如何修正嗎? 萬分感謝 -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 220.136.225.126

12/09 22:21, , 1F
要開 -std=c++11 然後用 C++11 的 template alias
12/09 22:21, 1F

12/09 22:21, , 2F
template <class T>
12/09 22:21, 2F

12/09 22:21, , 3F
using FunctionTypeA = void (*)(T& anITem);
12/09 22:21, 3F

12/09 22:37, , 4F
喔 可以跑了! 謝謝樓上! :)
12/09 22:37, 4F

12/09 22:43, , 5F
另外找到一個stackoverflow的相關討論 http://ppt.cc/rUFv
12/09 22:43, 5F

12/09 22:44, , 6F
供大家參考
12/09 22:44, 6F
文章代碼(AID): #1IfT0ZvA (C_and_CPP)