作者fuha (mimi)
看板Soft_Job
标题Re: [请益] MTK 面试测验请教
时间Sat Jun 12 21:53:01 2010
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
1F:→ ilovebbs:有点累.XD 06/12 21:54
2F:推 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)
3F:推 zerodevil:说的真轻松 你带带看啊XD 06/12 22:52
4F:推 loveflames:Functor<2>跟Functor<n>是不同的东西,前者在编译时就 06/12 22:54
5F:→ loveflames:知道了啊 06/12 22:54
6F:→ fuha:喔~对ㄟ~哈哈~ 06/12 22:55