→ 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