Re: [請益] MTK 面試測驗請教

看板Soft_Job (軟體人)作者 (mimi)時間15年前 (2010/06/12 21:53), 編輯推噓3(303)
留言6則, 5人參與, 最新討論串9/14 (看更多)
template <int> struct Functor{}; template <> struct Functor<1> { Functor() {func1();} }; template <> struct Functor<2> { Functor() {func2();} }; template <> struct Functor<3> { Functor() {func3();} }; template <> struct Functor<4> { Functor() {func4();} }; template <> struct Functor<5> { Functor() {func5();} }; int main(int n) { Functor<n>(); return 0; } 不過沒用到 if -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 114.26.165.141

06/12 21:54, , 1F
有點累.XD
06/12 21:54, 1F

06/12 22:08, , 2F
cool~!
06/12 22:08, 2F
loveflames:compile時不能知道n的值啊,template無法展開吧? 06/12 22:12 我貼我測試的code好了 #include <iostream> #define def_fcn(x) \ void func##x() \ { \ std::cout << #x; \ } def_fcn(1) def_fcn(2) def_fcn(3) def_fcn(4) def_fcn(5) template <int> struct Functor{}; #define def_call_fcn(x) \ template <> \ struct Functor<x> { Functor() {func##x();} }; def_call_fcn(1) def_call_fcn(2) def_call_fcn(3) def_call_fcn(4) def_call_fcn(5) int main() { Functor<2>(); return 0; } main 參數 n的部份 就自己帶囉~ ※ 編輯: fuha 來自: 114.26.165.141 (06/12 22:50)

06/12 22:52, , 3F
說的真輕鬆 你帶帶看啊XD
06/12 22:52, 3F

06/12 22:54, , 4F
Functor<2>跟Functor<n>是不同的東西,前者在編譯時就
06/12 22:54, 4F

06/12 22:54, , 5F
知道了啊
06/12 22:54, 5F

06/12 22:55, , 6F
喔~對ㄟ~哈哈~
06/12 22:55, 6F
文章代碼(AID): #1C4v50TJ (Soft_Job)
文章代碼(AID): #1C4v50TJ (Soft_Job)