Re: [請益] MTK 面試測驗請教
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
06/12 21:54, 1F
推
06/12 22:08, , 2F
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
06/12 22:52, 3F
推
06/12 22:54, , 4F
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
討論串 (同標題文章)
Soft_Job 近期熱門文章
29
63
PTT職涯區 即時熱門文章
60
144