比如我调用:
DUP_CALL(puts, "Hello, world!", 3);
编译器将会自动生成——
puts("Hello, world!");
puts("Hello, world!");
puts("Hello, world!");
decltype还不是太给力啊,还是typeof更好。
#include <stdio.h>
template <typename RET_TYPE, typename PARAM_TYPE, RET_TYPE FUNC(PARAM_TYPE), unsigned N>
struct DupStruct
{
static inline void DupOpCall(PARAM_TYPE param)
{
DupStruct<RET_TYPE, PARAM_TYPE, FUNC, N - 1>::DupOpCall(param);
FUNC(param);
}
};
template <typename RET_TYPE, typename PARAM_TYPE, RET_TYPE FUNC(PARAM_TYPE)>
struct DupStruct<RET_TYPE, PARAM_TYPE, FUNC, 0>
{
static inline void DupOpCall(PARAM_TYPE param)
{
}
};
#define DUP_CALL(func, param, nTimes) DupStruct<decltype(func(param)), decltype(param), func, nTimes>::DupOpCall(param)
int main(void)
{
DUP_CALL(puts, (const char*)"Hello, world!", 3);
}
int main(void)
{
00401000 push esi
DUP_CALL(puts, (const char*)"Hello, world!", 3);
00401001 mov esi,dword ptr [__imp__puts (4020A0h)]
00401007 push offset string "Hello, world!" (4020F4h)
0040100C call esi
0040100E push offset string "Hello, world!" (4020F4h)
00401013 call esi
00401015 push offset string "Hello, world!" (4020F4h)
0040101A call esi
0040101C add esp,0Ch
}
0040101F xor eax,eax
00401021 pop esi
00401022 ret
即然玩 C++11,何必还使用 C++03 的老旧套路呢。下面的实现更干净利落,而且还能接受任意参数。
#include <cstdio>
template <size_t N>
struct test
{
template <typename F, typename ... Ts>
static void repeat (F f, Ts ... ts)
{
f(ts...);
test<N-1>::repeat(f,ts...);
}
};
template <> struct test <0> { static void repeat (...) { } };
int main (int, char**)
{
test<3>::repeat(printf,"%s %s\n","hello","world");
return 0;
}
test<3>::repeat(printf,"%s %s\n","hello","world");
// g++ assembly
.LC0:
.string "world"
.LC1:
.string "hello"
.LC2:
.string "%s %s\n"
// a few lines omitted ...
movl $.LC0, 8(%esp)
movl $.LC1, 4(%esp)
movl $.LC2, (%esp)
call printf
movl $.LC0, 8(%esp)
movl $.LC1, 4(%esp)
movl $.LC2, (%esp)
call printf
movl $.LC0, 8(%esp)
movl $.LC1, 4(%esp)
movl $.LC2, (%esp)
call printf
首先说下,这不是我想出来的,TTL库的。我下的是1.3.2版
看了下,这个也不是无限制的,我下的库L里TTL_CNTDEC_x, TTL_REPEAT_x 这类的,x最大到512.
另外,这里有分析这玩意如何工作的。
http://www.kuqin.com/language/20080319/4797.html
vs2005,能编译过
#include <stdio.h>
#define TTL_CNTDEC_0 0
#define TTL_CNTDEC_1 0
#define TTL_CNTDEC_2 1
#define TTL_CNTDEC_3 2
#define TTL_REPEAT_0(m,l,p)
#define TTL_REPEAT_1(m,l,p) TTL_REPEAT_0(m,l,p) m(1,p)
#define TTL_REPEAT_2(m,l,p) TTL_REPEAT_1(m,l,p) m(2,p)
#define TTL_LAST_REPEAT_0(m,p)
#define TTL_LAST_REPEAT_1(m,p) m(1,p)
#define TTL_LAST_REPEAT_2(m,p) m(2,p)
#define TTL_LAST_REPEAT_3(m,p) m(3,p)
#define TTL_TPARAM_END(n,t) t;
#define TTL_APPEND1( x, y ) x ## y
#define TTL_APPEND( x, y ) TTL_APPEND1(x,y)
#define TTL_DEC(n) TTL_APPEND(TTL_CNTDEC_, n)
#define TTL_REPEAT(n, m, l, p) TTL_APPEND(TTL_REPEAT_, TTL_DEC(n))(m,l,p) TTL_APPEND(TTL_LAST_REPEAT_,n)(l,p)
#define TTL_TPARAM(n,t) t;
#define TTL_TPARAMSX(n,t) TTL_REPEAT(n, TTL_TPARAM, TTL_TPARAM_END, t)
#define TTL_TPARAMS(n) TTL_TPARAMSX(n,T)
int main(int argc, char *argv[])
{
TTL_TPARAMSX(3, puts("Hello"));
return 0;
}
参考:
http://topic.youkuaiyun.com/u/20110902/16/6a98bf07-284a-442b-986b-c240b13182d2.html