类型限定宏帮助程序



如何在限定类型时为繁琐的重复制作帮助程序宏?

std::is_function为例 -- 我将如何制作宏来帮助我节省编写所有这些可能的情况的时间?(显然,这不仅仅是一个C++问题,但在 C 语言中更易于管理(

这是我的想法,但它伤害了我的大脑(并且不起作用(:

#define empty_macro
#define finally_apply( macro, ... ) 
macro( __VA_ARGS__ )
#define apply_cv( macro, ... ) 
macro( __VA_ARGS__, empty_macro ) 
macro( __VA_ARGS__, const ) 
macro( __VA_ARGS__, volatile ) 
macro( __VA_ARGS__, const volatile )

#define apply_pointer( macro, ... ) 
macro( __VA_ARGS__, empty_macro ) 
macro( __VA_ARGS__, * )
// c++ only
#define apply_noexcept( macro, ... ) 
macro( __VA_ARGS__, empty_macro ) 
macro( __VA_ARGS__, noexcept )

我对使用像 Boost 这样的外部库不感兴趣。我想知道如何自己做。

有关详细信息,请参阅"可能的实现"。

您不必以统一的方式编写"cv限定符组合","noexcept-ness"等列表。

这是一种更简单的方法。这些宏生成std::is_function的专用化:

#define FUNC(a, b) 
struct is_function<Ret(Args... a) b> : std::true_type {};

#define FUNCLIST_D(c, v, ref, ne) 
FUNC(   , c v ref ne) 
FUNC(..., c v ref ne)

#define FUNCLIST_C(v, ref, ne) 
FUNCLIST_D(     , v, ref, ne) 
FUNCLIST_D(const, v, ref, ne)

#define FUNCLIST_B(ref, ne) 
FUNCLIST_C(        , ref, ne) 
FUNCLIST_C(volatile, ref, ne)

#define FUNCLIST_A(ne) 
FUNCLIST_B(  , ne) 
FUNCLIST_B(& , ne) 
FUNCLIST_B(&&, ne)

#define FUNCLIST 
FUNCLIST_A(        ) 
FUNCLIST_A(noexcept)
FUNCLIST

我希望我没有忘记任何事情。这将扩展到:

struct is_function<Ret(Args...)> : std::true_type {};
struct is_function<Ret(Args......)> : std::true_type {};
struct is_function<Ret(Args...) const> : std::true_type {};
struct is_function<Ret(Args......) const> : std::true_type {};
struct is_function<Ret(Args...) volatile> : std::true_type {};
struct is_function<Ret(Args......) volatile> : std::true_type {};
struct is_function<Ret(Args...) const volatile> : std::true_type {};
struct is_function<Ret(Args......) const volatile> : std::true_type {};
struct is_function<Ret(Args...) &> : std::true_type {};
struct is_function<Ret(Args......) &> : std::true_type {};
struct is_function<Ret(Args...) const &> : std::true_type {};
struct is_function<Ret(Args......) const &> : std::true_type {};
struct is_function<Ret(Args...) volatile &> : std::true_type {};
struct is_function<Ret(Args......) volatile &> : std::true_type {};
struct is_function<Ret(Args...) const volatile &> : std::true_type {};
struct is_function<Ret(Args......) const volatile &> : std::true_type {};
struct is_function<Ret(Args...) &&> : std::true_type {};
struct is_function<Ret(Args......) &&> : std::true_type {};
struct is_function<Ret(Args...) const &&> : std::true_type {};
struct is_function<Ret(Args......) const &&> : std::true_type {};
struct is_function<Ret(Args...) volatile &&> : std::true_type {};
struct is_function<Ret(Args......) volatile &&> : std::true_type {};
struct is_function<Ret(Args...) const volatile &&> : std::true_type {};
struct is_function<Ret(Args......) const volatile &&> : std::true_type {};
struct is_function<Ret(Args...) noexcept> : std::true_type {};
struct is_function<Ret(Args......) noexcept> : std::true_type {};
struct is_function<Ret(Args...) const noexcept> : std::true_type {};
struct is_function<Ret(Args......) const noexcept> : std::true_type {};
struct is_function<Ret(Args...) volatile noexcept> : std::true_type {};
struct is_function<Ret(Args......) volatile noexcept> : std::true_type {};
struct is_function<Ret(Args...) const volatile noexcept> : std::true_type {};
struct is_function<Ret(Args......) const volatile noexcept> : std::true_type {};
struct is_function<Ret(Args...) & noexcept> : std::true_type {};
struct is_function<Ret(Args......) & noexcept> : std::true_type {};
struct is_function<Ret(Args...) const &noexcept> : std::true_type {};
struct is_function<Ret(Args......) const &noexcept> : std::true_type {};
struct is_function<Ret(Args...) volatile &noexcept> : std::true_type {};
struct is_function<Ret(Args......) volatile &noexcept> : std::true_type {};
struct is_function<Ret(Args...) const volatile &noexcept> : std::true_type {};
struct is_function<Ret(Args......) const volatile &noexcept> : std::true_type {};
struct is_function<Ret(Args...) && noexcept> : std::true_type {};
struct is_function<Ret(Args......) && noexcept> : std::true_type {};
struct is_function<Ret(Args...) const &&noexcept> : std::true_type {};
struct is_function<Ret(Args......) const &&noexcept> : std::true_type {};
struct is_function<Ret(Args...) volatile &&noexcept> : std::true_type {};
struct is_function<Ret(Args......) volatile &&noexcept> : std::true_type {};
struct is_function<Ret(Args...) const volatile &&noexcept> : std::true_type {};
struct is_function<Ret(Args......) const volatile &&noexcept> : std::true_type {};

嗯,你要求它。

让我们首先从一个 FOREACH 宏开始,该宏允许自定义转义分隔符并转义其对大量参数的重载参数。我所说的"逃脱"是指大括号。有了这样的宏观M_FOREACHQS(macro, (separator), ((1), (2), (3)))会产生macro(1)separator macro(2)separator macro(3)(我希望(:

#define M_ESCAPE(...)  __VA_ARGS__
#define M_FOREACHQS_1(F,S,_1)  F(M_ESCAPE _1)
// seq 2 64 | while read l; do echo "#define M_FOREACHQS_$l(F,S,$(seq -f _%.0f $l | paste -sd ,)) \"; echo $'t'"M_FOREACHQS_$((l-1))(F,S,$(seq -f _%.0f $((l-1)) | paste -sd ,))M_ESCAPE S F(M_ESCAPE _$l)"; done
// TODO: Remove additional space before function, I guess with `##` or with empty `M_ESCAPE()`
#define M_FOREACHQS_2(F,S,_1,_2) 
M_FOREACHQS_1(F,S,_1)M_ESCAPE S F(M_ESCAPE _2)
#define M_FOREACHQS_3(F,S,_1,_2,_3) 
M_FOREACHQS_2(F,S,_1,_2)M_ESCAPE S F(M_ESCAPE _3)
#define M_FOREACHQS_4(F,S,_1,_2,_3,_4) 
M_FOREACHQS_3(F,S,_1,_2,_3)M_ESCAPE S F(M_ESCAPE _4)
#define M_FOREACHQS_5(F,S,_1,_2,_3,_4,_5) 
M_FOREACHQS_4(F,S,_1,_2,_3,_4)M_ESCAPE S F(M_ESCAPE _5)
#define M_FOREACHQS_6(F,S,_1,_2,_3,_4,_5,_6) 
M_FOREACHQS_5(F,S,_1,_2,_3,_4,_5)M_ESCAPE S F(M_ESCAPE _6)
#define M_FOREACHQS_7(F,S,_1,_2,_3,_4,_5,_6,_7) 
M_FOREACHQS_6(F,S,_1,_2,_3,_4,_5,_6)M_ESCAPE S F(M_ESCAPE _7)
#define M_FOREACHQS_8(F,S,_1,_2,_3,_4,_5,_6,_7,_8) 
M_FOREACHQS_7(F,S,_1,_2,_3,_4,_5,_6,_7)M_ESCAPE S F(M_ESCAPE _8)
#define M_FOREACHQS_9(F,S,_1,_2,_3,_4,_5,_6,_7,_8,_9) 
M_FOREACHQS_8(F,S,_1,_2,_3,_4,_5,_6,_7,_8)M_ESCAPE S F(M_ESCAPE _9)
#define M_FOREACHQS_10(F,S,_1,_2,_3,_4,_5,_6,_7,_8,_9,_10) 
M_FOREACHQS_9(F,S,_1,_2,_3,_4,_5,_6,_7,_8,_9)M_ESCAPE S F(M_ESCAPE _10)
#define M_FOREACHQS_11(F,S,_1,_2,_3,_4,_5,_6,_7,_8,_9,_10,_11) 
M_FOREACHQS_10(F,S,_1,_2,_3,_4,_5,_6,_7,_8,_9,_10)M_ESCAPE S F(M_ESCAPE _11)
#define M_FOREACHQS_12(F,S,_1,_2,_3,_4,_5,_6,_7,_8,_9,_10,_11,_12) 
M_FOREACHQS_11(F,S,_1,_2,_3,_4,_5,_6,_7,_8,_9,_10,_11)M_ESCAPE S F(M_ESCAPE _12)
#define M_FOREACHQS_13(F,S,_1,_2,_3,_4,_5,_6,_7,_8,_9,_10,_11,_12,_13) 
M_FOREACHQS_12(F,S,_1,_2,_3,_4,_5,_6,_7,_8,_9,_10,_11,_12)M_ESCAPE S F(M_ESCAPE _13)
#define M_FOREACHQS_14(F,S,_1,_2,_3,_4,_5,_6,_7,_8,_9,_10,_11,_12,_13,_14) 
M_FOREACHQS_13(F,S,_1,_2,_3,_4,_5,_6,_7,_8,_9,_10,_11,_12,_13)M_ESCAPE S F(M_ESCAPE _14)
#define M_FOREACHQS_15(F,S,_1,_2,_3,_4,_5,_6,_7,_8,_9,_10,_11,_12,_13,_14,_15) 
M_FOREACHQS_14(F,S,_1,_2,_3,_4,_5,_6,_7,_8,_9,_10,_11,_12,_13,_14)M_ESCAPE S F(M_ESCAPE _15)
#define M_FOREACHQS_16(F,S,_1,_2,_3,_4,_5,_6,_7,_8,_9,_10,_11,_12,_13,_14,_15,_16) 
M_FOREACHQS_15(F,S,_1,_2,_3,_4,_5,_6,_7,_8,_9,_10,_11,_12,_13,_14,_15)M_ESCAPE S F(M_ESCAPE _16)
#define M_FOREACHQS_17(F,S,_1,_2,_3,_4,_5,_6,_7,_8,_9,_10,_11,_12,_13,_14,_15,_16,_17) 
M_FOREACHQS_16(F,S,_1,_2,_3,_4,_5,_6,_7,_8,_9,_10,_11,_12,_13,_14,_15,_16)M_ESCAPE S F(M_ESCAPE _17)
#define M_FOREACHQS_18(F,S,_1,_2,_3,_4,_5,_6,_7,_8,_9,_10,_11,_12,_13,_14,_15,_16,_17,_18) 
M_FOREACHQS_17(F,S,_1,_2,_3,_4,_5,_6,_7,_8,_9,_10,_11,_12,_13,_14,_15,_16,_17)M_ESCAPE S F(M_ESCAPE _18)
#define M_FOREACHQS_19(F,S,_1,_2,_3,_4,_5,_6,_7,_8,_9,_10,_11,_12,_13,_14,_15,_16,_17,_18,_19) 
M_FOREACHQS_18(F,S,_1,_2,_3,_4,_5,_6,_7,_8,_9,_10,_11,_12,_13,_14,_15,_16,_17,_18)M_ESCAPE S F(M_ESCAPE _19)
#define M_FOREACHQS_20(F,S,_1,_2,_3,_4,_5,_6,_7,_8,_9,_10,_11,_12,_13,_14,_15,_16,_17,_18,_19,_20) 
M_FOREACHQS_19(F,S,_1,_2,_3,_4,_5,_6,_7,_8,_9,_10,_11,_12,_13,_14,_15,_16,_17,_18,_19)M_ESCAPE S F(M_ESCAPE _20)
#define M_FOREACHQS_21(F,S,_1,_2,_3,_4,_5,_6,_7,_8,_9,_10,_11,_12,_13,_14,_15,_16,_17,_18,_19,_20,_21) 
M_FOREACHQS_20(F,S,_1,_2,_3,_4,_5,_6,_7,_8,_9,_10,_11,_12,_13,_14,_15,_16,_17,_18,_19,_20)M_ESCAPE S F(M_ESCAPE _21)
#define M_FOREACHQS_22(F,S,_1,_2,_3,_4,_5,_6,_7,_8,_9,_10,_11,_12,_13,_14,_15,_16,_17,_18,_19,_20,_21,_22) 
M_FOREACHQS_21(F,S,_1,_2,_3,_4,_5,_6,_7,_8,_9,_10,_11,_12,_13,_14,_15,_16,_17,_18,_19,_20,_21)M_ESCAPE S F(M_ESCAPE _22)
#define M_FOREACHQS_23(F,S,_1,_2,_3,_4,_5,_6,_7,_8,_9,_10,_11,_12,_13,_14,_15,_16,_17,_18,_19,_20,_21,_22,_23) 
M_FOREACHQS_22(F,S,_1,_2,_3,_4,_5,_6,_7,_8,_9,_10,_11,_12,_13,_14,_15,_16,_17,_18,_19,_20,_21,_22)M_ESCAPE S F(M_ESCAPE _23)
#define M_FOREACHQS_24(F,S,_1,_2,_3,_4,_5,_6,_7,_8,_9,_10,_11,_12,_13,_14,_15,_16,_17,_18,_19,_20,_21,_22,_23,_24) 
M_FOREACHQS_23(F,S,_1,_2,_3,_4,_5,_6,_7,_8,_9,_10,_11,_12,_13,_14,_15,_16,_17,_18,_19,_20,_21,_22,_23)M_ESCAPE S F(M_ESCAPE _24)
#define M_FOREACHQS_25(F,S,_1,_2,_3,_4,_5,_6,_7,_8,_9,_10,_11,_12,_13,_14,_15,_16,_17,_18,_19,_20,_21,_22,_23,_24,_25) 
M_FOREACHQS_24(F,S,_1,_2,_3,_4,_5,_6,_7,_8,_9,_10,_11,_12,_13,_14,_15,_16,_17,_18,_19,_20,_21,_22,_23,_24)M_ESCAPE S F(M_ESCAPE _25)
#define M_FOREACHQS_26(F,S,_1,_2,_3,_4,_5,_6,_7,_8,_9,_10,_11,_12,_13,_14,_15,_16,_17,_18,_19,_20,_21,_22,_23,_24,_25,_26) 
M_FOREACHQS_25(F,S,_1,_2,_3,_4,_5,_6,_7,_8,_9,_10,_11,_12,_13,_14,_15,_16,_17,_18,_19,_20,_21,_22,_23,_24,_25)M_ESCAPE S F(M_ESCAPE _26)
#define M_FOREACHQS_27(F,S,_1,_2,_3,_4,_5,_6,_7,_8,_9,_10,_11,_12,_13,_14,_15,_16,_17,_18,_19,_20,_21,_22,_23,_24,_25,_26,_27) 
M_FOREACHQS_26(F,S,_1,_2,_3,_4,_5,_6,_7,_8,_9,_10,_11,_12,_13,_14,_15,_16,_17,_18,_19,_20,_21,_22,_23,_24,_25,_26)M_ESCAPE S F(M_ESCAPE _27)
#define M_FOREACHQS_28(F,S,_1,_2,_3,_4,_5,_6,_7,_8,_9,_10,_11,_12,_13,_14,_15,_16,_17,_18,_19,_20,_21,_22,_23,_24,_25,_26,_27,_28) 
M_FOREACHQS_27(F,S,_1,_2,_3,_4,_5,_6,_7,_8,_9,_10,_11,_12,_13,_14,_15,_16,_17,_18,_19,_20,_21,_22,_23,_24,_25,_26,_27)M_ESCAPE S F(M_ESCAPE _28)
#define M_FOREACHQS_29(F,S,_1,_2,_3,_4,_5,_6,_7,_8,_9,_10,_11,_12,_13,_14,_15,_16,_17,_18,_19,_20,_21,_22,_23,_24,_25,_26,_27,_28,_29) 
M_FOREACHQS_28(F,S,_1,_2,_3,_4,_5,_6,_7,_8,_9,_10,_11,_12,_13,_14,_15,_16,_17,_18,_19,_20,_21,_22,_23,_24,_25,_26,_27,_28)M_ESCAPE S F(M_ESCAPE _29)
#define M_FOREACHQS_30(F,S,_1,_2,_3,_4,_5,_6,_7,_8,_9,_10,_11,_12,_13,_14,_15,_16,_17,_18,_19,_20,_21,_22,_23,_24,_25,_26,_27,_28,_29,_30) 
M_FOREACHQS_29(F,S,_1,_2,_3,_4,_5,_6,_7,_8,_9,_10,_11,_12,_13,_14,_15,_16,_17,_18,_19,_20,_21,_22,_23,_24,_25,_26,_27,_28,_29)M_ESCAPE S F(M_ESCAPE _30)
#define M_FOREACHQS_31(F,S,_1,_2,_3,_4,_5,_6,_7,_8,_9,_10,_11,_12,_13,_14,_15,_16,_17,_18,_19,_20,_21,_22,_23,_24,_25,_26,_27,_28,_29,_30,_31) 
M_FOREACHQS_30(F,S,_1,_2,_3,_4,_5,_6,_7,_8,_9,_10,_11,_12,_13,_14,_15,_16,_17,_18,_19,_20,_21,_22,_23,_24,_25,_26,_27,_28,_29,_30)M_ESCAPE S F(M_ESCAPE _31)
#define M_FOREACHQS_32(F,S,_1,_2,_3,_4,_5,_6,_7,_8,_9,_10,_11,_12,_13,_14,_15,_16,_17,_18,_19,_20,_21,_22,_23,_24,_25,_26,_27,_28,_29,_30,_31,_32) 
M_FOREACHQS_31(F,S,_1,_2,_3,_4,_5,_6,_7,_8,_9,_10,_11,_12,_13,_14,_15,_16,_17,_18,_19,_20,_21,_22,_23,_24,_25,_26,_27,_28,_29,_30,_31)M_ESCAPE S F(M_ESCAPE _32)
#define M_FOREACHQS_33(F,S,_1,_2,_3,_4,_5,_6,_7,_8,_9,_10,_11,_12,_13,_14,_15,_16,_17,_18,_19,_20,_21,_22,_23,_24,_25,_26,_27,_28,_29,_30,_31,_32,_33) 
M_FOREACHQS_32(F,S,_1,_2,_3,_4,_5,_6,_7,_8,_9,_10,_11,_12,_13,_14,_15,_16,_17,_18,_19,_20,_21,_22,_23,_24,_25,_26,_27,_28,_29,_30,_31,_32)M_ESCAPE S F(M_ESCAPE _33)
#define M_FOREACHQS_34(F,S,_1,_2,_3,_4,_5,_6,_7,_8,_9,_10,_11,_12,_13,_14,_15,_16,_17,_18,_19,_20,_21,_22,_23,_24,_25,_26,_27,_28,_29,_30,_31,_32,_33,_34) 
M_FOREACHQS_33(F,S,_1,_2,_3,_4,_5,_6,_7,_8,_9,_10,_11,_12,_13,_14,_15,_16,_17,_18,_19,_20,_21,_22,_23,_24,_25,_26,_27,_28,_29,_30,_31,_32,_33)M_ESCAPE S F(M_ESCAPE _34)
#define M_FOREACHQS_35(F,S,_1,_2,_3,_4,_5,_6,_7,_8,_9,_10,_11,_12,_13,_14,_15,_16,_17,_18,_19,_20,_21,_22,_23,_24,_25,_26,_27,_28,_29,_30,_31,_32,_33,_34,_35) 
M_FOREACHQS_34(F,S,_1,_2,_3,_4,_5,_6,_7,_8,_9,_10,_11,_12,_13,_14,_15,_16,_17,_18,_19,_20,_21,_22,_23,_24,_25,_26,_27,_28,_29,_30,_31,_32,_33,_34)M_ESCAPE S F(M_ESCAPE _35)
#define M_FOREACHQS_36(F,S,_1,_2,_3,_4,_5,_6,_7,_8,_9,_10,_11,_12,_13,_14,_15,_16,_17,_18,_19,_20,_21,_22,_23,_24,_25,_26,_27,_28,_29,_30,_31,_32,_33,_34,_35,_36) 
M_FOREACHQS_35(F,S,_1,_2,_3,_4,_5,_6,_7,_8,_9,_10,_11,_12,_13,_14,_15,_16,_17,_18,_19,_20,_21,_22,_23,_24,_25,_26,_27,_28,_29,_30,_31,_32,_33,_34,_35)M_ESCAPE S F(M_ESCAPE _36)
#define M_FOREACHQS_37(F,S,_1,_2,_3,_4,_5,_6,_7,_8,_9,_10,_11,_12,_13,_14,_15,_16,_17,_18,_19,_20,_21,_22,_23,_24,_25,_26,_27,_28,_29,_30,_31,_32,_33,_34,_35,_36,_37) 
M_FOREACHQS_36(F,S,_1,_2,_3,_4,_5,_6,_7,_8,_9,_10,_11,_12,_13,_14,_15,_16,_17,_18,_19,_20,_21,_22,_23,_24,_25,_26,_27,_28,_29,_30,_31,_32,_33,_34,_35,_36)M_ESCAPE S F(M_ESCAPE _37)
#define M_FOREACHQS_38(F,S,_1,_2,_3,_4,_5,_6,_7,_8,_9,_10,_11,_12,_13,_14,_15,_16,_17,_18,_19,_20,_21,_22,_23,_24,_25,_26,_27,_28,_29,_30,_31,_32,_33,_34,_35,_36,_37,_38) 
M_FOREACHQS_37(F,S,_1,_2,_3,_4,_5,_6,_7,_8,_9,_10,_11,_12,_13,_14,_15,_16,_17,_18,_19,_20,_21,_22,_23,_24,_25,_26,_27,_28,_29,_30,_31,_32,_33,_34,_35,_36,_37)M_ESCAPE S F(M_ESCAPE _38)
#define M_FOREACHQS_39(F,S,_1,_2,_3,_4,_5,_6,_7,_8,_9,_10,_11,_12,_13,_14,_15,_16,_17,_18,_19,_20,_21,_22,_23,_24,_25,_26,_27,_28,_29,_30,_31,_32,_33,_34,_35,_36,_37,_38,_39) 
M_FOREACHQS_38(F,S,_1,_2,_3,_4,_5,_6,_7,_8,_9,_10,_11,_12,_13,_14,_15,_16,_17,_18,_19,_20,_21,_22,_23,_24,_25,_26,_27,_28,_29,_30,_31,_32,_33,_34,_35,_36,_37,_38)M_ESCAPE S F(M_ESCAPE _39)
#define M_FOREACHQS_40(F,S,_1,_2,_3,_4,_5,_6,_7,_8,_9,_10,_11,_12,_13,_14,_15,_16,_17,_18,_19,_20,_21,_22,_23,_24,_25,_26,_27,_28,_29,_30,_31,_32,_33,_34,_35,_36,_37,_38,_39,_40) 
M_FOREACHQS_39(F,S,_1,_2,_3,_4,_5,_6,_7,_8,_9,_10,_11,_12,_13,_14,_15,_16,_17,_18,_19,_20,_21,_22,_23,_24,_25,_26,_27,_28,_29,_30,_31,_32,_33,_34,_35,_36,_37,_38,_39)M_ESCAPE S F(M_ESCAPE _40)
#define M_FOREACHQS_41(F,S,_1,_2,_3,_4,_5,_6,_7,_8,_9,_10,_11,_12,_13,_14,_15,_16,_17,_18,_19,_20,_21,_22,_23,_24,_25,_26,_27,_28,_29,_30,_31,_32,_33,_34,_35,_36,_37,_38,_39,_40,_41) 
M_FOREACHQS_40(F,S,_1,_2,_3,_4,_5,_6,_7,_8,_9,_10,_11,_12,_13,_14,_15,_16,_17,_18,_19,_20,_21,_22,_23,_24,_25,_26,_27,_28,_29,_30,_31,_32,_33,_34,_35,_36,_37,_38,_39,_40)M_ESCAPE S F(M_ESCAPE _41)
#define M_FOREACHQS_42(F,S,_1,_2,_3,_4,_5,_6,_7,_8,_9,_10,_11,_12,_13,_14,_15,_16,_17,_18,_19,_20,_21,_22,_23,_24,_25,_26,_27,_28,_29,_30,_31,_32,_33,_34,_35,_36,_37,_38,_39,_40,_41,_42) 
M_FOREACHQS_41(F,S,_1,_2,_3,_4,_5,_6,_7,_8,_9,_10,_11,_12,_13,_14,_15,_16,_17,_18,_19,_20,_21,_22,_23,_24,_25,_26,_27,_28,_29,_30,_31,_32,_33,_34,_35,_36,_37,_38,_39,_40,_41)M_ESCAPE S F(M_ESCAPE _42)
#define M_FOREACHQS_43(F,S,_1,_2,_3,_4,_5,_6,_7,_8,_9,_10,_11,_12,_13,_14,_15,_16,_17,_18,_19,_20,_21,_22,_23,_24,_25,_26,_27,_28,_29,_30,_31,_32,_33,_34,_35,_36,_37,_38,_39,_40,_41,_42,_43) 
M_FOREACHQS_42(F,S,_1,_2,_3,_4,_5,_6,_7,_8,_9,_10,_11,_12,_13,_14,_15,_16,_17,_18,_19,_20,_21,_22,_23,_24,_25,_26,_27,_28,_29,_30,_31,_32,_33,_34,_35,_36,_37,_38,_39,_40,_41,_42)M_ESCAPE S F(M_ESCAPE _43)
#define M_FOREACHQS_44(F,S,_1,_2,_3,_4,_5,_6,_7,_8,_9,_10,_11,_12,_13,_14,_15,_16,_17,_18,_19,_20,_21,_22,_23,_24,_25,_26,_27,_28,_29,_30,_31,_32,_33,_34,_35,_36,_37,_38,_39,_40,_41,_42,_43,_44) 
M_FOREACHQS_43(F,S,_1,_2,_3,_4,_5,_6,_7,_8,_9,_10,_11,_12,_13,_14,_15,_16,_17,_18,_19,_20,_21,_22,_23,_24,_25,_26,_27,_28,_29,_30,_31,_32,_33,_34,_35,_36,_37,_38,_39,_40,_41,_42,_43)M_ESCAPE S F(M_ESCAPE _44)
#define M_FOREACHQS_45(F,S,_1,_2,_3,_4,_5,_6,_7,_8,_9,_10,_11,_12,_13,_14,_15,_16,_17,_18,_19,_20,_21,_22,_23,_24,_25,_26,_27,_28,_29,_30,_31,_32,_33,_34,_35,_36,_37,_38,_39,_40,_41,_42,_43,_44,_45) 
M_FOREACHQS_44(F,S,_1,_2,_3,_4,_5,_6,_7,_8,_9,_10,_11,_12,_13,_14,_15,_16,_17,_18,_19,_20,_21,_22,_23,_24,_25,_26,_27,_28,_29,_30,_31,_32,_33,_34,_35,_36,_37,_38,_39,_40,_41,_42,_43,_44)M_ESCAPE S F(M_ESCAPE _45)
#define M_FOREACHQS_46(F,S,_1,_2,_3,_4,_5,_6,_7,_8,_9,_10,_11,_12,_13,_14,_15,_16,_17,_18,_19,_20,_21,_22,_23,_24,_25,_26,_27,_28,_29,_30,_31,_32,_33,_34,_35,_36,_37,_38,_39,_40,_41,_42,_43,_44,_45,_46) 
M_FOREACHQS_45(F,S,_1,_2,_3,_4,_5,_6,_7,_8,_9,_10,_11,_12,_13,_14,_15,_16,_17,_18,_19,_20,_21,_22,_23,_24,_25,_26,_27,_28,_29,_30,_31,_32,_33,_34,_35,_36,_37,_38,_39,_40,_41,_42,_43,_44,_45)M_ESCAPE S F(M_ESCAPE _46)
#define M_FOREACHQS_47(F,S,_1,_2,_3,_4,_5,_6,_7,_8,_9,_10,_11,_12,_13,_14,_15,_16,_17,_18,_19,_20,_21,_22,_23,_24,_25,_26,_27,_28,_29,_30,_31,_32,_33,_34,_35,_36,_37,_38,_39,_40,_41,_42,_43,_44,_45,_46,_47) 
M_FOREACHQS_46(F,S,_1,_2,_3,_4,_5,_6,_7,_8,_9,_10,_11,_12,_13,_14,_15,_16,_17,_18,_19,_20,_21,_22,_23,_24,_25,_26,_27,_28,_29,_30,_31,_32,_33,_34,_35,_36,_37,_38,_39,_40,_41,_42,_43,_44,_45,_46)M_ESCAPE S F(M_ESCAPE _47)
#define M_FOREACHQS_48(F,S,_1,_2,_3,_4,_5,_6,_7,_8,_9,_10,_11,_12,_13,_14,_15,_16,_17,_18,_19,_20,_21,_22,_23,_24,_25,_26,_27,_28,_29,_30,_31,_32,_33,_34,_35,_36,_37,_38,_39,_40,_41,_42,_43,_44,_45,_46,_47,_48) 
M_FOREACHQS_47(F,S,_1,_2,_3,_4,_5,_6,_7,_8,_9,_10,_11,_12,_13,_14,_15,_16,_17,_18,_19,_20,_21,_22,_23,_24,_25,_26,_27,_28,_29,_30,_31,_32,_33,_34,_35,_36,_37,_38,_39,_40,_41,_42,_43,_44,_45,_46,_47)M_ESCAPE S F(M_ESCAPE _48)
#define M_FOREACHQS_49(F,S,_1,_2,_3,_4,_5,_6,_7,_8,_9,_10,_11,_12,_13,_14,_15,_16,_17,_18,_19,_20,_21,_22,_23,_24,_25,_26,_27,_28,_29,_30,_31,_32,_33,_34,_35,_36,_37,_38,_39,_40,_41,_42,_43,_44,_45,_46,_47,_48,_49) 
M_FOREACHQS_48(F,S,_1,_2,_3,_4,_5,_6,_7,_8,_9,_10,_11,_12,_13,_14,_15,_16,_17,_18,_19,_20,_21,_22,_23,_24,_25,_26,_27,_28,_29,_30,_31,_32,_33,_34,_35,_36,_37,_38,_39,_40,_41,_42,_43,_44,_45,_46,_47,_48)M_ESCAPE S F(M_ESCAPE _49)
#define M_FOREACHQS_50(F,S,_1,_2,_3,_4,_5,_6,_7,_8,_9,_10,_11,_12,_13,_14,_15,_16,_17,_18,_19,_20,_21,_22,_23,_24,_25,_26,_27,_28,_29,_30,_31,_32,_33,_34,_35,_36,_37,_38,_39,_40,_41,_42,_43,_44,_45,_46,_47,_48,_49,_50) 
M_FOREACHQS_49(F,S,_1,_2,_3,_4,_5,_6,_7,_8,_9,_10,_11,_12,_13,_14,_15,_16,_17,_18,_19,_20,_21,_22,_23,_24,_25,_26,_27,_28,_29,_30,_31,_32,_33,_34,_35,_36,_37,_38,_39,_40,_41,_42,_43,_44,_45,_46,_47,_48,_49)M_ESCAPE S F(M_ESCAPE _50)
#define M_FOREACHQS_51(F,S,_1,_2,_3,_4,_5,_6,_7,_8,_9,_10,_11,_12,_13,_14,_15,_16,_17,_18,_19,_20,_21,_22,_23,_24,_25,_26,_27,_28,_29,_30,_31,_32,_33,_34,_35,_36,_37,_38,_39,_40,_41,_42,_43,_44,_45,_46,_47,_48,_49,_50,_51) 
M_FOREACHQS_50(F,S,_1,_2,_3,_4,_5,_6,_7,_8,_9,_10,_11,_12,_13,_14,_15,_16,_17,_18,_19,_20,_21,_22,_23,_24,_25,_26,_27,_28,_29,_30,_31,_32,_33,_34,_35,_36,_37,_38,_39,_40,_41,_42,_43,_44,_45,_46,_47,_48,_49,_50)M_ESCAPE S F(M_ESCAPE _51)
#define M_FOREACHQS_52(F,S,_1,_2,_3,_4,_5,_6,_7,_8,_9,_10,_11,_12,_13,_14,_15,_16,_17,_18,_19,_20,_21,_22,_23,_24,_25,_26,_27,_28,_29,_30,_31,_32,_33,_34,_35,_36,_37,_38,_39,_40,_41,_42,_43,_44,_45,_46,_47,_48,_49,_50,_51,_52) 
M_FOREACHQS_51(F,S,_1,_2,_3,_4,_5,_6,_7,_8,_9,_10,_11,_12,_13,_14,_15,_16,_17,_18,_19,_20,_21,_22,_23,_24,_25,_26,_27,_28,_29,_30,_31,_32,_33,_34,_35,_36,_37,_38,_39,_40,_41,_42,_43,_44,_45,_46,_47,_48,_49,_50,_51)M_ESCAPE S F(M_ESCAPE _52)
#define M_FOREACHQS_53(F,S,_1,_2,_3,_4,_5,_6,_7,_8,_9,_10,_11,_12,_13,_14,_15,_16,_17,_18,_19,_20,_21,_22,_23,_24,_25,_26,_27,_28,_29,_30,_31,_32,_33,_34,_35,_36,_37,_38,_39,_40,_41,_42,_43,_44,_45,_46,_47,_48,_49,_50,_51,_52,_53) 
M_FOREACHQS_52(F,S,_1,_2,_3,_4,_5,_6,_7,_8,_9,_10,_11,_12,_13,_14,_15,_16,_17,_18,_19,_20,_21,_22,_23,_24,_25,_26,_27,_28,_29,_30,_31,_32,_33,_34,_35,_36,_37,_38,_39,_40,_41,_42,_43,_44,_45,_46,_47,_48,_49,_50,_51,_52)M_ESCAPE S F(M_ESCAPE _53)
#define M_FOREACHQS_54(F,S,_1,_2,_3,_4,_5,_6,_7,_8,_9,_10,_11,_12,_13,_14,_15,_16,_17,_18,_19,_20,_21,_22,_23,_24,_25,_26,_27,_28,_29,_30,_31,_32,_33,_34,_35,_36,_37,_38,_39,_40,_41,_42,_43,_44,_45,_46,_47,_48,_49,_50,_51,_52,_53,_54) 
M_FOREACHQS_53(F,S,_1,_2,_3,_4,_5,_6,_7,_8,_9,_10,_11,_12,_13,_14,_15,_16,_17,_18,_19,_20,_21,_22,_23,_24,_25,_26,_27,_28,_29,_30,_31,_32,_33,_34,_35,_36,_37,_38,_39,_40,_41,_42,_43,_44,_45,_46,_47,_48,_49,_50,_51,_52,_53)M_ESCAPE S F(M_ESCAPE _54)
#define M_FOREACHQS_55(F,S,_1,_2,_3,_4,_5,_6,_7,_8,_9,_10,_11,_12,_13,_14,_15,_16,_17,_18,_19,_20,_21,_22,_23,_24,_25,_26,_27,_28,_29,_30,_31,_32,_33,_34,_35,_36,_37,_38,_39,_40,_41,_42,_43,_44,_45,_46,_47,_48,_49,_50,_51,_52,_53,_54,_55) 
M_FOREACHQS_54(F,S,_1,_2,_3,_4,_5,_6,_7,_8,_9,_10,_11,_12,_13,_14,_15,_16,_17,_18,_19,_20,_21,_22,_23,_24,_25,_26,_27,_28,_29,_30,_31,_32,_33,_34,_35,_36,_37,_38,_39,_40,_41,_42,_43,_44,_45,_46,_47,_48,_49,_50,_51,_52,_53,_54)M_ESCAPE S F(M_ESCAPE _55)
#define M_FOREACHQS_56(F,S,_1,_2,_3,_4,_5,_6,_7,_8,_9,_10,_11,_12,_13,_14,_15,_16,_17,_18,_19,_20,_21,_22,_23,_24,_25,_26,_27,_28,_29,_30,_31,_32,_33,_34,_35,_36,_37,_38,_39,_40,_41,_42,_43,_44,_45,_46,_47,_48,_49,_50,_51,_52,_53,_54,_55,_56) 
M_FOREACHQS_55(F,S,_1,_2,_3,_4,_5,_6,_7,_8,_9,_10,_11,_12,_13,_14,_15,_16,_17,_18,_19,_20,_21,_22,_23,_24,_25,_26,_27,_28,_29,_30,_31,_32,_33,_34,_35,_36,_37,_38,_39,_40,_41,_42,_43,_44,_45,_46,_47,_48,_49,_50,_51,_52,_53,_54,_55)M_ESCAPE S F(M_ESCAPE _56)
#define M_FOREACHQS_57(F,S,_1,_2,_3,_4,_5,_6,_7,_8,_9,_10,_11,_12,_13,_14,_15,_16,_17,_18,_19,_20,_21,_22,_23,_24,_25,_26,_27,_28,_29,_30,_31,_32,_33,_34,_35,_36,_37,_38,_39,_40,_41,_42,_43,_44,_45,_46,_47,_48,_49,_50,_51,_52,_53,_54,_55,_56,_57) 
M_FOREACHQS_56(F,S,_1,_2,_3,_4,_5,_6,_7,_8,_9,_10,_11,_12,_13,_14,_15,_16,_17,_18,_19,_20,_21,_22,_23,_24,_25,_26,_27,_28,_29,_30,_31,_32,_33,_34,_35,_36,_37,_38,_39,_40,_41,_42,_43,_44,_45,_46,_47,_48,_49,_50,_51,_52,_53,_54,_55,_56)M_ESCAPE S F(M_ESCAPE _57)
#define M_FOREACHQS_58(F,S,_1,_2,_3,_4,_5,_6,_7,_8,_9,_10,_11,_12,_13,_14,_15,_16,_17,_18,_19,_20,_21,_22,_23,_24,_25,_26,_27,_28,_29,_30,_31,_32,_33,_34,_35,_36,_37,_38,_39,_40,_41,_42,_43,_44,_45,_46,_47,_48,_49,_50,_51,_52,_53,_54,_55,_56,_57,_58) 
M_FOREACHQS_57(F,S,_1,_2,_3,_4,_5,_6,_7,_8,_9,_10,_11,_12,_13,_14,_15,_16,_17,_18,_19,_20,_21,_22,_23,_24,_25,_26,_27,_28,_29,_30,_31,_32,_33,_34,_35,_36,_37,_38,_39,_40,_41,_42,_43,_44,_45,_46,_47,_48,_49,_50,_51,_52,_53,_54,_55,_56,_57)M_ESCAPE S F(M_ESCAPE _58)
#define M_FOREACHQS_59(F,S,_1,_2,_3,_4,_5,_6,_7,_8,_9,_10,_11,_12,_13,_14,_15,_16,_17,_18,_19,_20,_21,_22,_23,_24,_25,_26,_27,_28,_29,_30,_31,_32,_33,_34,_35,_36,_37,_38,_39,_40,_41,_42,_43,_44,_45,_46,_47,_48,_49,_50,_51,_52,_53,_54,_55,_56,_57,_58,_59) 
M_FOREACHQS_58(F,S,_1,_2,_3,_4,_5,_6,_7,_8,_9,_10,_11,_12,_13,_14,_15,_16,_17,_18,_19,_20,_21,_22,_23,_24,_25,_26,_27,_28,_29,_30,_31,_32,_33,_34,_35,_36,_37,_38,_39,_40,_41,_42,_43,_44,_45,_46,_47,_48,_49,_50,_51,_52,_53,_54,_55,_56,_57,_58)M_ESCAPE S F(M_ESCAPE _59)
#define M_FOREACHQS_60(F,S,_1,_2,_3,_4,_5,_6,_7,_8,_9,_10,_11,_12,_13,_14,_15,_16,_17,_18,_19,_20,_21,_22,_23,_24,_25,_26,_27,_28,_29,_30,_31,_32,_33,_34,_35,_36,_37,_38,_39,_40,_41,_42,_43,_44,_45,_46,_47,_48,_49,_50,_51,_52,_53,_54,_55,_56,_57,_58,_59,_60) 
M_FOREACHQS_59(F,S,_1,_2,_3,_4,_5,_6,_7,_8,_9,_10,_11,_12,_13,_14,_15,_16,_17,_18,_19,_20,_21,_22,_23,_24,_25,_26,_27,_28,_29,_30,_31,_32,_33,_34,_35,_36,_37,_38,_39,_40,_41,_42,_43,_44,_45,_46,_47,_48,_49,_50,_51,_52,_53,_54,_55,_56,_57,_58,_59)M_ESCAPE S F(M_ESCAPE _60)
#define M_FOREACHQS_61(F,S,_1,_2,_3,_4,_5,_6,_7,_8,_9,_10,_11,_12,_13,_14,_15,_16,_17,_18,_19,_20,_21,_22,_23,_24,_25,_26,_27,_28,_29,_30,_31,_32,_33,_34,_35,_36,_37,_38,_39,_40,_41,_42,_43,_44,_45,_46,_47,_48,_49,_50,_51,_52,_53,_54,_55,_56,_57,_58,_59,_60,_61) 
M_FOREACHQS_60(F,S,_1,_2,_3,_4,_5,_6,_7,_8,_9,_10,_11,_12,_13,_14,_15,_16,_17,_18,_19,_20,_21,_22,_23,_24,_25,_26,_27,_28,_29,_30,_31,_32,_33,_34,_35,_36,_37,_38,_39,_40,_41,_42,_43,_44,_45,_46,_47,_48,_49,_50,_51,_52,_53,_54,_55,_56,_57,_58,_59,_60)M_ESCAPE S F(M_ESCAPE _61)
#define M_FOREACHQS_62(F,S,_1,_2,_3,_4,_5,_6,_7,_8,_9,_10,_11,_12,_13,_14,_15,_16,_17,_18,_19,_20,_21,_22,_23,_24,_25,_26,_27,_28,_29,_30,_31,_32,_33,_34,_35,_36,_37,_38,_39,_40,_41,_42,_43,_44,_45,_46,_47,_48,_49,_50,_51,_52,_53,_54,_55,_56,_57,_58,_59,_60,_61,_62) 
M_FOREACHQS_61(F,S,_1,_2,_3,_4,_5,_6,_7,_8,_9,_10,_11,_12,_13,_14,_15,_16,_17,_18,_19,_20,_21,_22,_23,_24,_25,_26,_27,_28,_29,_30,_31,_32,_33,_34,_35,_36,_37,_38,_39,_40,_41,_42,_43,_44,_45,_46,_47,_48,_49,_50,_51,_52,_53,_54,_55,_56,_57,_58,_59,_60,_61)M_ESCAPE S F(M_ESCAPE _62)
#define M_FOREACHQS_63(F,S,_1,_2,_3,_4,_5,_6,_7,_8,_9,_10,_11,_12,_13,_14,_15,_16,_17,_18,_19,_20,_21,_22,_23,_24,_25,_26,_27,_28,_29,_30,_31,_32,_33,_34,_35,_36,_37,_38,_39,_40,_41,_42,_43,_44,_45,_46,_47,_48,_49,_50,_51,_52,_53,_54,_55,_56,_57,_58,_59,_60,_61,_62,_63) 
M_FOREACHQS_62(F,S,_1,_2,_3,_4,_5,_6,_7,_8,_9,_10,_11,_12,_13,_14,_15,_16,_17,_18,_19,_20,_21,_22,_23,_24,_25,_26,_27,_28,_29,_30,_31,_32,_33,_34,_35,_36,_37,_38,_39,_40,_41,_42,_43,_44,_45,_46,_47,_48,_49,_50,_51,_52,_53,_54,_55,_56,_57,_58,_59,_60,_61,_62)M_ESCAPE S F(M_ESCAPE _63)
#define M_FOREACHQS_64(F,S,_1,_2,_3,_4,_5,_6,_7,_8,_9,_10,_11,_12,_13,_14,_15,_16,_17,_18,_19,_20,_21,_22,_23,_24,_25,_26,_27,_28,_29,_30,_31,_32,_33,_34,_35,_36,_37,_38,_39,_40,_41,_42,_43,_44,_45,_46,_47,_48,_49,_50,_51,_52,_53,_54,_55,_56,_57,_58,_59,_60,_61,_62,_63,_64) 
M_FOREACHQS_63(F,S,_1,_2,_3,_4,_5,_6,_7,_8,_9,_10,_11,_12,_13,_14,_15,_16,_17,_18,_19,_20,_21,_22,_23,_24,_25,_26,_27,_28,_29,_30,_31,_32,_33,_34,_35,_36,_37,_38,_39,_40,_41,_42,_43,_44,_45,_46,_47,_48,_49,_50,_51,_52,_53,_54,_55,_56,_57,_58,_59,_60,_61,_62,_63)M_ESCAPE S F(M_ESCAPE _64)
// seq 64 | tac | paste -sd ,
#define M_FOREACHQS_N(_1,_2,_3,_4,_5,_6,_7,_8,_9,_10,_11,_12,_13,_14,_15,_16,_17,_18,_19,_20,_21,_22,_23,_24,_25,_26,_27,_28,_29,_30,_31,_32,_33,_34,_35,_36,_37,_38,_39,_40,_41,_42,_43,_44,_45,_46,_47,_48,_49,_50,_51,_52,_53,_54,_55,_56,_57,_58,_59,_60,_61,_62,_63,_64,N,...) 
M_FOREACHQS_##N
// seq 64 | tac | paste -sd ,
#define M_FOREACHQS(F, S, ...) 
M_FOREACHQS_N(__VA_ARGS__,64,63,62,61,60,59,58,57,56,55,54,53,52,51,50,49,48,47,46,45,44,43,42,41,40,39,38,37,36,35,34,33,32,31,30,29,28,27,26,25,24,23,22,21,20,19,18,17,16,15,14,13,12,11,10,9,8,7,6,5,4,3,2,1)(F, S, __VA_ARGS__)

我们将在某个宏中生成函数定义,该宏采用两个参数 - 函数参数列表的可变参数...部分和类型:

// needs another expansion wave, cause it's applied on `(..., const)`.
#define M_DEFINE_IS_FUNCTION(...) 
M_DEFINE_IS_FUNCTION_IN(__VA_ARGS__)
#define M_DEFINE_IS_FUNCTION_IN(VAR, TYPE) 
template<class Ret, class... Args> 
struct is_function<Ret(Args... VAR) TYPE> : true_type {};

一些基本定义让我们入门:

// Meeeeeeeeeeeeeeeeeep
struct true_type { static constexpr bool value = true; };
struct false_type { static constexpr bool value = false; };
template<class> struct is_function : false_type { };

然后我们必须生成限定符的可能组合的每个排列(我永远不知道它是如何称呼的(,noexcept,&&或&或什么都没有,可变参数或不是参数列表。有两个列表(a, b)(c, d)我想有一个列表(a c, a d, b c, b d)并执行 4 次。我使用一组应用于彼此列表的重载宏来做到这一点,结果再次应用于上面的定义宏:

// Honestly, this could be generalized too...
#define M_CONST_APPLY(...) 
(__VA_ARGS__), 
(__VA_ARGS__ const)
#define M_VOLATILE_APPLY(...) 
(__VA_ARGS__ ), 
(__VA_ARGS__ volatile)
#define M_REFS_APPLY(...)  
(__VA_ARGS__ ), 
(__VA_ARGS__ &), 
(__VA_ARGS__ &&)
#define M_NOEXCEPT_APPLY(...)  
(__VA_ARGS__), 
(__VA_ARGS__ noexcept)
M_FOREACHQS(M_DEFINE_IS_FUNCTION, (),
M_FOREACHQS(M_NOEXCEPT_APPLY, (,),
M_FOREACHQS(M_REFS_APPLY, (,),
M_FOREACHQS(M_VOLATILE_APPLY, (,),
M_FOREACHQS(M_CONST_APPLY, (,),
// Arguments list - empty or ... variadic function
// We do two arguments here
(, ),
(..., )
)
)
)
)
)
int main() {
return is_function<int()>::value;
}

和神电链接。老实说,当您只键入所有可能的组合时,代码更容易维护、管理和理解。

最新更新