看板 Soft_Job 關於我們 聯絡資訊
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
ilovebbs:有點累.XD 06/12 21:54
yourinfo:cool~! 06/12 22:08
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)
zerodevil:說的真輕鬆 你帶帶看啊XD 06/12 22:52
loveflames:Functor<2>跟Functor<n>是不同的東西,前者在編譯時就 06/12 22:54
loveflames:知道了啊 06/12 22:54
fuha:喔~對ㄟ~哈哈~ 06/12 22:55