[問題] template function pointer

看板C_and_CPP (C/C++)作者 (New CygnusX)時間15年前 (2011/01/18 19:59), 編輯推噓1(103)
留言4則, 3人參與, 最新討論串1/2 (看更多)
程式碼如下: (C++ primer 3rd 中文版 侯捷譯 p.587) template<typename Type, bool (*Comp) (const Type&, const Type &)> const Type& min (const Type *p, int size, Comp comp) //此行錯誤 { //do compare operation } 錯誤訊息: D:\Test\c++\HelloWorld\main.cpp|25|error: 'Comp' is not a type| ||=== Build finished: 1 errors, 0 warnings ===| 我照書上打的, 看起來Comp要這樣寫: typedef bool (*Comp) (const Type&, const Type &); 但是不清楚怎麼放進去template 請問要怎麼改才會對? -- ╠═╬══╬═╬═╬═╣ ║ ║ ║ ║╲║╱║ ║ ║ ║ 此時, ╠═╬═╬══╬═╬═╬═╬═╣ 帥有何用? ║ ║ ║ ║╱║╲║ ║ ║ ║ 有車又如何? ╚═╩═╩═╩═╩═╩═╩═╩═╝ 有馬子又怎樣? ψKnicks : 你錯了,當你有車跟帥的時候,馬是可以變成炮 卒就沒用 -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 180.176.8.219

01/18 20:03, , 1F
就 typename Comp 就好了
01/18 20:03, 1F

01/18 20:03, , 2F
能不能呼叫編譯器會處理
01/18 20:03, 2F

01/18 20:43, , 3F
這樣寫只能接函數指標耶 XD
01/18 20:43, 3F

01/19 00:18, , 4F
我以為 template 內只能放 typename or 泛整數型別
01/19 00:18, 4F
文章代碼(AID): #1DDO2kcQ (C_and_CPP)
討論串 (同標題文章)
文章代碼(AID): #1DDO2kcQ (C_and_CPP)