引子 最近准备重构一下我的kapok库,让meta函数可以返回元素为kv的tuple,例如: - <font color="black"><span class="hljs-keyword" style="box-sizing: border-box;">struct</span> person
- {
- <span class="hljs-built_in" style="box-sizing: border-box;">std</span>::<span class="hljs-built_in" style="box-sizing: border-box;">string</span> name;
- <span class="hljs-keyword" style="box-sizing: border-box;">int</span> age;
- META(name, age) <span class="hljs-comment" style="box-sizing: border-box;">//定义一个支持变参的meta函数</span>
- };
- <span class="hljs-keyword" style="box-sizing: border-box;">int</span> main()
- {
- person p = {“tom”, <span class="hljs-number" style="box-sizing: border-box;">20</span>};
- <span class="hljs-keyword" style="box-sizing: border-box;">auto</span> tp = p.meta();
- <span class="hljs-keyword" style="box-sizing: border-box;">static_assert</span>(<span class="hljs-built_in" style="box-sizing: border-box;">std</span>::is_same(<span class="hljs-built_in" style="box-sizing: border-box;">std</span>::tuple<<span class="hljs-built_in" style="box-sizing: border-box;">std</span>::pair<<span class="hljs-built_in" style="box-sizing: border-box;">std</span>::<span class="hljs-built_in" style="box-sizing: border-box;">string</span>, <span class="hljs-keyword" style="box-sizing: border-box;">int</span>>>, <span class="hljs-keyword" style="box-sizing: border-box;">decltype</span>(tp), “not same”);
- <span class="hljs-comment" style="box-sizing: border-box;">//tp在内存中是这样的 {{“name”:”tom”}, {“age”:20}};</span>
- <span class="hljs-keyword" style="box-sizing: border-box;">return</span> <span class="hljs-number" style="box-sizing: border-box;">0</span>;
- }</font>
复制代码
类似这个META的实现我在msgpack的库里看到了,在这里: 仅仅是宏元的代码就数百行了,看起来非常复杂,msgpack之所以用这么复杂的方式去实现恐怕是为了支持c++98/03标准。本来想看看msgpack是如何实现META函数的,但是它的宏元代码读起来比较困难,于是作罢。 后来想起群里的ddrm实现了类似的功能,据说没有msgpack这么复杂,简洁一些,于是向ddrm要来了代码(在此感谢ddrm分享的源码)。他的代码也是用宏元,但是比msgpack的代码少很多,将近一百行代码。不过,我不太喜欢这么复杂的代码,我希望用一种更简单的方式去实现这个效果。这里附上ddrm的代码,大家可以借鉴参考一下。 - <font color="black"><span class="hljs-id" style="box-sizing: border-box;">#ifndef</span> TUPLE_MACRO_DEF_H
- <span class="hljs-id" style="box-sizing: border-box;">#define</span> TUPLE_MACRO_DEF_H
- <span class="hljs-id" style="box-sizing: border-box;">#define</span> <span class="hljs-function" style="box-sizing: border-box;">MARCO_EXPAND(arg_list)</span> arg_list
- <span class="hljs-id" style="box-sizing: border-box;">#define</span> <span class="hljs-function" style="box-sizing: border-box;">APPLY_VARIADIC_MACRO(macro,tuple)</span> macro tuple
- <span class="hljs-id" style="box-sizing: border-box;">#define</span> <span class="hljs-function" style="box-sizing: border-box;">ADD_REFERENCE(t)</span> std<span class="hljs-value" style="box-sizing: border-box;">::reference_wrapper<decltype(t)>(t)
- <span class="hljs-hexcolor" style="box-sizing: border-box;">#def</span>ine ADD_REFERENCE_CONST(t) std::reference_wrapper<std::add_const_t<decltype(t)>>(t)
- <span class="hljs-hexcolor" style="box-sizing: border-box;">#def</span>ine PAIR_OBJECT(t) std::make_pair(#t, ADD_REFERENCE(t))
- <span class="hljs-hexcolor" style="box-sizing: border-box;">#def</span>ine PAIR_OBJECT_CONST(t) std::make_pair(#t, ADD_REFERENCE_CONST(t))
- <span class="hljs-hexcolor" style="box-sizing: border-box;">#def</span>ine MAKE_TUPLE(...) auto tuple() { return std::make_tuple(__VA_ARGS__);</span> }
- <span class="hljs-id" style="box-sizing: border-box;">#define</span> <span class="hljs-function" style="box-sizing: border-box;">MAKE_TUPLE_CONST(...)</span> <span class="hljs-attribute" style="box-sizing: border-box;">auto</span> <span class="hljs-function" style="box-sizing: border-box;">tuple()</span> const { return std<span class="hljs-value" style="box-sizing: border-box;">::make_tuple(__VA_ARGS__);</span> }
- <span class="hljs-comment" style="box-sizing: border-box;">/* arg list expand macro, now support 40 args */</span>
- <span class="hljs-id" style="box-sizing: border-box;">#define</span> <span class="hljs-function" style="box-sizing: border-box;">MAKE_ARG_LIST_1(op, arg, ...)</span> <span class="hljs-function" style="box-sizing: border-box;">op(arg)</span>
- <span class="hljs-id" style="box-sizing: border-box;">#define</span> <span class="hljs-function" style="box-sizing: border-box;">MAKE_ARG_LIST_2(op, arg, ...)</span> <span class="hljs-function" style="box-sizing: border-box;">op(arg)</span>, <span class="hljs-function" style="box-sizing: border-box;">MARCO_EXPAND(<span class="hljs-function" style="box-sizing: border-box;">MAKE_ARG_LIST_1(op, __VA_ARGS__)</span>)</span>
- <span class="hljs-id" style="box-sizing: border-box;">#define</span> <span class="hljs-function" style="box-sizing: border-box;">MAKE_ARG_LIST_3(op, arg, ...)</span> <span class="hljs-function" style="box-sizing: border-box;">op(arg)</span>, <span class="hljs-function" style="box-sizing: border-box;">MARCO_EXPAND(<span class="hljs-function" style="box-sizing: border-box;">MAKE_ARG_LIST_2(op, __VA_ARGS__)</span>)</span>
- <span class="hljs-id" style="box-sizing: border-box;">#define</span> <span class="hljs-function" style="box-sizing: border-box;">MAKE_ARG_LIST_4(op, arg, ...)</span> <span class="hljs-function" style="box-sizing: border-box;">op(arg)</span>, <span class="hljs-function" style="box-sizing: border-box;">MARCO_EXPAND(<span class="hljs-function" style="box-sizing: border-box;">MAKE_ARG_LIST_3(op, __VA_ARGS__)</span>)</span>
- <span class="hljs-id" style="box-sizing: border-box;">#define</span> <span class="hljs-function" style="box-sizing: border-box;">MAKE_ARG_LIST_5(op, arg, ...)</span> <span class="hljs-function" style="box-sizing: border-box;">op(arg)</span>, <span class="hljs-function" style="box-sizing: border-box;">MARCO_EXPAND(<span class="hljs-function" style="box-sizing: border-box;">MAKE_ARG_LIST_4(op, __VA_ARGS__)</span>)</span>
- <span class="hljs-id" style="box-sizing: border-box;">#define</span> <span class="hljs-function" style="box-sizing: border-box;">MAKE_ARG_LIST_6(op, arg, ...)</span> <span class="hljs-function" style="box-sizing: border-box;">op(arg)</span>, <span class="hljs-function" style="box-sizing: border-box;">MARCO_EXPAND(<span class="hljs-function" style="box-sizing: border-box;">MAKE_ARG_LIST_5(op, __VA_ARGS__)</span>)</span>
- <span class="hljs-id" style="box-sizing: border-box;">#define</span> <span class="hljs-function" style="box-sizing: border-box;">MAKE_ARG_LIST_7(op, arg, ...)</span> <span class="hljs-function" style="box-sizing: border-box;">op(arg)</span>, <span class="hljs-function" style="box-sizing: border-box;">MARCO_EXPAND(<span class="hljs-function" style="box-sizing: border-box;">MAKE_ARG_LIST_6(op, __VA_ARGS__)</span>)</span>
- <span class="hljs-id" style="box-sizing: border-box;">#define</span> <span class="hljs-function" style="box-sizing: border-box;">MAKE_ARG_LIST_8(op, arg, ...)</span> <span class="hljs-function" style="box-sizing: border-box;">op(arg)</span>, <span class="hljs-function" style="box-sizing: border-box;">MARCO_EXPAND(<span class="hljs-function" style="box-sizing: border-box;">MAKE_ARG_LIST_7(op, __VA_ARGS__)</span>)</span>
- <span class="hljs-id" style="box-sizing: border-box;">#define</span> <span class="hljs-function" style="box-sizing: border-box;">MAKE_ARG_LIST_9(op, arg, ...)</span> <span class="hljs-function" style="box-sizing: border-box;">op(arg)</span>, <span class="hljs-function" style="box-sizing: border-box;">MARCO_EXPAND(<span class="hljs-function" style="box-sizing: border-box;">MAKE_ARG_LIST_8(op, __VA_ARGS__)</span>)</span>
- <span class="hljs-id" style="box-sizing: border-box;">#define</span> <span class="hljs-function" style="box-sizing: border-box;">MAKE_ARG_LIST_10(op, arg, ...)</span> <span class="hljs-function" style="box-sizing: border-box;">op(arg)</span>, <span class="hljs-function" style="box-sizing: border-box;">MARCO_EXPAND(<span class="hljs-function" style="box-sizing: border-box;">MAKE_ARG_LIST_9(op, __VA_ARGS__)</span>)</span>
- <span class="hljs-id" style="box-sizing: border-box;">#define</span> <span class="hljs-function" style="box-sizing: border-box;">MAKE_ARG_LIST_11(op, arg, ...)</span> <span class="hljs-function" style="box-sizing: border-box;">op(arg)</span>, <span class="hljs-function" style="box-sizing: border-box;">MARCO_EXPAND(<span class="hljs-function" style="box-sizing: border-box;">MAKE_ARG_LIST_10(op, __VA_ARGS__)</span>)</span>
- <span class="hljs-id" style="box-sizing: border-box;">#define</span> <span class="hljs-function" style="box-sizing: border-box;">MAKE_ARG_LIST_12(op, arg, ...)</span> <span class="hljs-function" style="box-sizing: border-box;">op(arg)</span>, <span class="hljs-function" style="box-sizing: border-box;">MARCO_EXPAND(<span class="hljs-function" style="box-sizing: border-box;">MAKE_ARG_LIST_11(op, __VA_ARGS__)</span>)</span>
- <span class="hljs-id" style="box-sizing: border-box;">#define</span> <span class="hljs-function" style="box-sizing: border-box;">MAKE_ARG_LIST_13(op, arg, ...)</span> <span class="hljs-function" style="box-sizing: border-box;">op(arg)</span>, <span class="hljs-function" style="box-sizing: border-box;">MARCO_EXPAND(<span class="hljs-function" style="box-sizing: border-box;">MAKE_ARG_LIST_12(op, __VA_ARGS__)</span>)</span>
- <span class="hljs-id" style="box-sizing: border-box;">#define</span> <span class="hljs-function" style="box-sizing: border-box;">MAKE_ARG_LIST_14(op, arg, ...)</span> <span class="hljs-function" style="box-sizing: border-box;">op(arg)</span>, <span class="hljs-function" style="box-sizing: border-box;">MARCO_EXPAND(<span class="hljs-function" style="box-sizing: border-box;">MAKE_ARG_LIST_13(op, __VA_ARGS__)</span>)</span>
- <span class="hljs-id" style="box-sizing: border-box;">#define</span> <span class="hljs-function" style="box-sizing: border-box;">MAKE_ARG_LIST_15(op, arg, ...)</span> <span class="hljs-function" style="box-sizing: border-box;">op(arg)</span>, <span class="hljs-function" style="box-sizing: border-box;">MARCO_EXPAND(<span class="hljs-function" style="box-sizing: border-box;">MAKE_ARG_LIST_14(op, __VA_ARGS__)</span>)</span>
- <span class="hljs-id" style="box-sizing: border-box;">#define</span> <span class="hljs-function" style="box-sizing: border-box;">MAKE_ARG_LIST_16(op, arg, ...)</span> <span class="hljs-function" style="box-sizing: border-box;">op(arg)</span>, <span class="hljs-function" style="box-sizing: border-box;">MARCO_EXPAND(<span class="hljs-function" style="box-sizing: border-box;">MAKE_ARG_LIST_15(op, __VA_ARGS__)</span>)</span>
- <span class="hljs-id" style="box-sizing: border-box;">#define</span> <span class="hljs-function" style="box-sizing: border-box;">MAKE_ARG_LIST_17(op, arg, ...)</span> <span class="hljs-function" style="box-sizing: border-box;">op(arg)</span>, <span class="hljs-function" style="box-sizing: border-box;">MARCO_EXPAND(<span class="hljs-function" style="box-sizing: border-box;">MAKE_ARG_LIST_16(op, __VA_ARGS__)</span>)</span>
- <span class="hljs-id" style="box-sizing: border-box;">#define</span> <span class="hljs-function" style="box-sizing: border-box;">MAKE_ARG_LIST_18(op, arg, ...)</span> <span class="hljs-function" style="box-sizing: border-box;">op(arg)</span>, <span class="hljs-function" style="box-sizing: border-box;">MARCO_EXPAND(<span class="hljs-function" style="box-sizing: border-box;">MAKE_ARG_LIST_17(op, __VA_ARGS__)</span>)</span>
- <span class="hljs-id" style="box-sizing: border-box;">#define</span> <span class="hljs-function" style="box-sizing: border-box;">MAKE_ARG_LIST_19(op, arg, ...)</span> <span class="hljs-function" style="box-sizing: border-box;">op(arg)</span>, <span class="hljs-function" style="box-sizing: border-box;">MARCO_EXPAND(<span class="hljs-function" style="box-sizing: border-box;">MAKE_ARG_LIST_18(op, __VA_ARGS__)</span>)</span>
- <span class="hljs-id" style="box-sizing: border-box;">#define</span> <span class="hljs-function" style="box-sizing: border-box;">MAKE_ARG_LIST_20(op, arg, ...)</span> <span class="hljs-function" style="box-sizing: border-box;">op(arg)</span>, <span class="hljs-function" style="box-sizing: border-box;">MARCO_EXPAND(<span class="hljs-function" style="box-sizing: border-box;">MAKE_ARG_LIST_19(op, __VA_ARGS__)</span>)</span>
- <span class="hljs-id" style="box-sizing: border-box;">#define</span> <span class="hljs-function" style="box-sizing: border-box;">MAKE_ARG_LIST_21(op, arg, ...)</span> <span class="hljs-function" style="box-sizing: border-box;">op(arg)</span>, <span class="hljs-function" style="box-sizing: border-box;">MARCO_EXPAND(<span class="hljs-function" style="box-sizing: border-box;">MAKE_ARG_LIST_20(op, __VA_ARGS__)</span>)</span>
- <span class="hljs-id" style="box-sizing: border-box;">#define</span> <span class="hljs-function" style="box-sizing: border-box;">MAKE_ARG_LIST_22(op, arg, ...)</span> <span class="hljs-function" style="box-sizing: border-box;">op(arg)</span>, <span class="hljs-function" style="box-sizing: border-box;">MARCO_EXPAND(<span class="hljs-function" style="box-sizing: border-box;">MAKE_ARG_LIST_21(op, __VA_ARGS__)</span>)</span>
- <span class="hljs-id" style="box-sizing: border-box;">#define</span> <span class="hljs-function" style="box-sizing: border-box;">MAKE_ARG_LIST_23(op, arg, ...)</span> <span class="hljs-function" style="box-sizing: border-box;">op(arg)</span>, <span class="hljs-function" style="box-sizing: border-box;">MARCO_EXPAND(<span class="hljs-function" style="box-sizing: border-box;">MAKE_ARG_LIST_22(op, __VA_ARGS__)</span>)</span>
- <span class="hljs-id" style="box-sizing: border-box;">#define</span> <span class="hljs-function" style="box-sizing: border-box;">MAKE_ARG_LIST_24(op, arg, ...)</span> <span class="hljs-function" style="box-sizing: border-box;">op(arg)</span>, <span class="hljs-function" style="box-sizing: border-box;">MARCO_EXPAND(<span class="hljs-function" style="box-sizing: border-box;">MAKE_ARG_LIST_23(op, __VA_ARGS__)</span>)</span>
- <span class="hljs-id" style="box-sizing: border-box;">#define</span> <span class="hljs-function" style="box-sizing: border-box;">MAKE_ARG_LIST_25(op, arg, ...)</span> <span class="hljs-function" style="box-sizing: border-box;">op(arg)</span>, <span class="hljs-function" style="box-sizing: border-box;">MARCO_EXPAND(<span class="hljs-function" style="box-sizing: border-box;">MAKE_ARG_LIST_24(op, __VA_ARGS__)</span>)</span>
- <span class="hljs-id" style="box-sizing: border-box;">#define</span> <span class="hljs-function" style="box-sizing: border-box;">MAKE_ARG_LIST_26(op, arg, ...)</span> <span class="hljs-function" style="box-sizing: border-box;">op(arg)</span>, <span class="hljs-function" style="box-sizing: border-box;">MARCO_EXPAND(<span class="hljs-function" style="box-sizing: border-box;">MAKE_ARG_LIST_25(op, __VA_ARGS__)</span>)</span>
- <span class="hljs-id" style="box-sizing: border-box;">#define</span> <span class="hljs-function" style="box-sizing: border-box;">MAKE_ARG_LIST_27(op, arg, ...)</span> <span class="hljs-function" style="box-sizing: border-box;">op(arg)</span>, <span class="hljs-function" style="box-sizing: border-box;">MARCO_EXPAND(<span class="hljs-function" style="box-sizing: border-box;">MAKE_ARG_LIST_26(op, __VA_ARGS__)</span>)</span>
- <span class="hljs-id" style="box-sizing: border-box;">#define</span> <span class="hljs-function" style="box-sizing: border-box;">MAKE_ARG_LIST_28(op, arg, ...)</span> <span class="hljs-function" style="box-sizing: border-box;">op(arg)</span>, <span class="hljs-function" style="box-sizing: border-box;">MARCO_EXPAND(<span class="hljs-function" style="box-sizing: border-box;">MAKE_ARG_LIST_27(op, __VA_ARGS__)</span>)</span>
- <span class="hljs-id" style="box-sizing: border-box;">#define</span> <span class="hljs-function" style="box-sizing: border-box;">MAKE_ARG_LIST_29(op, arg, ...)</span> <span class="hljs-function" style="box-sizing: border-box;">op(arg)</span>, <span class="hljs-function" style="box-sizing: border-box;">MARCO_EXPAND(<span class="hljs-function" style="box-sizing: border-box;">MAKE_ARG_LIST_28(op, __VA_ARGS__)</span>)</span>
- <span class="hljs-id" style="box-sizing: border-box;">#define</span> <span class="hljs-function" style="box-sizing: border-box;">MAKE_ARG_LIST_30(op, arg, ...)</span> <span class="hljs-function" style="box-sizing: border-box;">op(arg)</span>, <span class="hljs-function" style="box-sizing: border-box;">MARCO_EXPAND(<span class="hljs-function" style="box-sizing: border-box;">MAKE_ARG_LIST_29(op, __VA_ARGS__)</span>)</span>
- <span class="hljs-id" style="box-sizing: border-box;">#define</span> <span class="hljs-function" style="box-sizing: border-box;">MAKE_ARG_LIST_31(op, arg, ...)</span> <span class="hljs-function" style="box-sizing: border-box;">op(arg)</span>, <span class="hljs-function" style="box-sizing: border-box;">MARCO_EXPAND(<span class="hljs-function" style="box-sizing: border-box;">MAKE_ARG_LIST_30(op, __VA_ARGS__)</span>)</span>
- <span class="hljs-id" style="box-sizing: border-box;">#define</span> <span class="hljs-function" style="box-sizing: border-box;">MAKE_ARG_LIST_32(op, arg, ...)</span> <span class="hljs-function" style="box-sizing: border-box;">op(arg)</span>, <span class="hljs-function" style="box-sizing: border-box;">MARCO_EXPAND(<span class="hljs-function" style="box-sizing: border-box;">MAKE_ARG_LIST_31(op, __VA_ARGS__)</span>)</span>
- <span class="hljs-id" style="box-sizing: border-box;">#define</span> <span class="hljs-function" style="box-sizing: border-box;">MAKE_ARG_LIST_33(op, arg, ...)</span> <span class="hljs-function" style="box-sizing: border-box;">op(arg)</span>, <span class="hljs-function" style="box-sizing: border-box;">MARCO_EXPAND(<span class="hljs-function" style="box-sizing: border-box;">MAKE_ARG_LIST_32(op, __VA_ARGS__)</span>)</span>
- <span class="hljs-id" style="box-sizing: border-box;">#define</span> <span class="hljs-function" style="box-sizing: border-box;">MAKE_ARG_LIST_34(op, arg, ...)</span> <span class="hljs-function" style="box-sizing: border-box;">op(arg)</span>, <span class="hljs-function" style="box-sizing: border-box;">MARCO_EXPAND(<span class="hljs-function" style="box-sizing: border-box;">MAKE_ARG_LIST_33(op, __VA_ARGS__)</span>)</span>
- <span class="hljs-id" style="box-sizing: border-box;">#define</span> <span class="hljs-function" style="box-sizing: border-box;">MAKE_ARG_LIST_35(op, arg, ...)</span> <span class="hljs-function" style="box-sizing: border-box;">op(arg)</span>, <span class="hljs-function" style="box-sizing: border-box;">MARCO_EXPAND(<span class="hljs-function" style="box-sizing: border-box;">MAKE_ARG_LIST_34(op, __VA_ARGS__)</span>)</span>
- <span class="hljs-id" style="box-sizing: border-box;">#define</span> <span class="hljs-function" style="box-sizing: border-box;">MAKE_ARG_LIST_36(op, arg, ...)</span> <span class="hljs-function" style="box-sizing: border-box;">op(arg)</span>, <span class="hljs-function" style="box-sizing: border-box;">MARCO_EXPAND(<span class="hljs-function" style="box-sizing: border-box;">MAKE_ARG_LIST_35(op, __VA_ARGS__)</span>)</span>
- <span class="hljs-id" style="box-sizing: border-box;">#define</span> <span class="hljs-function" style="box-sizing: border-box;">MAKE_ARG_LIST_37(op, arg, ...)</span> <span class="hljs-function" style="box-sizing: border-box;">op(arg)</span>, <span class="hljs-function" style="box-sizing: border-box;">MARCO_EXPAND(<span class="hljs-function" style="box-sizing: border-box;">MAKE_ARG_LIST_36(op, __VA_ARGS__)</span>)</span>
- <span class="hljs-id" style="box-sizing: border-box;">#define</span> <span class="hljs-function" style="box-sizing: border-box;">MAKE_ARG_LIST_38(op, arg, ...)</span> <span class="hljs-function" style="box-sizing: border-box;">op(arg)</span>, <span class="hljs-function" style="box-sizing: border-box;">MARCO_EXPAND(<span class="hljs-function" style="box-sizing: border-box;">MAKE_ARG_LIST_37(op, __VA_ARGS__)</span>)</span>
- <span class="hljs-id" style="box-sizing: border-box;">#define</span> <span class="hljs-function" style="box-sizing: border-box;">MAKE_ARG_LIST_39(op, arg, ...)</span> <span class="hljs-function" style="box-sizing: border-box;">op(arg)</span>, <span class="hljs-function" style="box-sizing: border-box;">MARCO_EXPAND(<span class="hljs-function" style="box-sizing: border-box;">MAKE_ARG_LIST_38(op, __VA_ARGS__)</span>)</span>
- <span class="hljs-id" style="box-sizing: border-box;">#define</span> <span class="hljs-function" style="box-sizing: border-box;">MAKE_ARG_LIST_40(op, arg, ...)</span> <span class="hljs-function" style="box-sizing: border-box;">op(arg)</span>, <span class="hljs-function" style="box-sizing: border-box;">MARCO_EXPAND(<span class="hljs-function" style="box-sizing: border-box;">MAKE_ARG_LIST_39(op, __VA_ARGS__)</span>)</span>
- <span class="hljs-comment" style="box-sizing: border-box;">/* emmbed marco, using EMMBED_TUPLE(5 , a, b, c, d, e) */</span>
- <span class="hljs-comment" style="box-sizing: border-box;">//note use MACRO_CONCAT like A##_##B direct may cause marco expand error</span>
- <span class="hljs-id" style="box-sizing: border-box;">#define</span> <span class="hljs-function" style="box-sizing: border-box;">MACRO_CONCAT(A, B)</span> <span class="hljs-function" style="box-sizing: border-box;">MACRO_CONCAT1(A, B)</span>
- <span class="hljs-id" style="box-sizing: border-box;">#define</span> <span class="hljs-function" style="box-sizing: border-box;">MACRO_CONCAT1(A, B)</span> <span class="hljs-tag" style="box-sizing: border-box;">A</span>#<span class="hljs-id" style="box-sizing: border-box;">#_</span>#<span class="hljs-id" style="box-sizing: border-box;">#B</span>
- <span class="hljs-id" style="box-sizing: border-box;">#define</span> <span class="hljs-function" style="box-sizing: border-box;">MAKE_ARG_LIST(N, op, arg, ...)</span> \
- <span class="hljs-function" style="box-sizing: border-box;">MACRO_CONCAT(MAKE_ARG_LIST, N)</span>(op, arg, __VA_ARGS__)
- <span class="hljs-id" style="box-sizing: border-box;">#define</span> <span class="hljs-function" style="box-sizing: border-box;">EMMBED_TUPLE(N, ...)</span> \
- <span class="hljs-function" style="box-sizing: border-box;">MAKE_TUPLE(<span class="hljs-function" style="box-sizing: border-box;">MAKE_ARG_LIST(N, PAIR_OBJECT, __VA_ARGS__)</span>)</span> \
- <span class="hljs-function" style="box-sizing: border-box;">MAKE_TUPLE_CONST(<span class="hljs-function" style="box-sizing: border-box;">MAKE_ARG_LIST(N, PAIR_OBJECT_CONST, __VA_ARGS__)</span>)</span>
- <span class="hljs-comment" style="box-sizing: border-box;">// see http://groups.google.com/group/comp.std.c/browse_thread/thread/77ee8c8f92e4a3fb/346fc464319b1ee5</span>
- <span class="hljs-id" style="box-sizing: border-box;">#define</span> <span class="hljs-function" style="box-sizing: border-box;">RSEQ_N()</span> \
- 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,0
- <span class="hljs-id" style="box-sizing: border-box;">#define</span> <span class="hljs-function" style="box-sizing: border-box;">ARG_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,N, ...)</span> N
- <span class="hljs-id" style="box-sizing: border-box;">#define</span> <span class="hljs-function" style="box-sizing: border-box;">GET_ARG_COUNT(...)</span> <span class="hljs-function" style="box-sizing: border-box;">APPLY_VARIADIC_MACRO(GET_ARG_COUNT_INNER,(__VA_ARGS__, <span class="hljs-function" style="box-sizing: border-box;">RSEQ_N()</span>)</span>)
- <span class="hljs-id" style="box-sizing: border-box;">#define</span> <span class="hljs-function" style="box-sizing: border-box;">GET_ARG_COUNT_INNER(...)</span> <span class="hljs-function" style="box-sizing: border-box;">APPLY_VARIADIC_MACRO(ARG_N, (__VA_ARGS__)</span>)
- <span class="hljs-id" style="box-sizing: border-box;">#define</span> <span class="hljs-function" style="box-sizing: border-box;">EMMBED(...)</span> <span class="hljs-function" style="box-sizing: border-box;">EMMBED_TUPLE(<span class="hljs-function" style="box-sizing: border-box;">GET_ARG_COUNT(__VA_ARGS__)</span>, __VA_ARGS__)</span>
- <span class="hljs-id" style="box-sizing: border-box;">#endif</span></font>
复制代码
10行代码解决问题我探索了一些可能的方案后,最终找到了一种满意的方案,最终的实现代码不过十来行,非常简洁,感谢c++11/14,正是现代C++才让代码变得如此简洁。 实现思路也比较简单,问题的本质是将输入的参数值变成一个pair,key是值的字面量,value是变量本身的值,所以生成的pair是这样的std::pair< std::string, someval >, 另外这个pair是每一个输入参数值都会生成一个pair,如果是一系列的参数值就要生成一系列的pair,这就要求支持变参。所以问题的关键是要在展开变参的过程中将这些pair放到一个tuple中,这对于c++11/14来说是不难办到的,下面是实现代码: - <font color="black"><span class="hljs-preprocessor" style="box-sizing: border-box;">#define MAKE_PAIR(text) std::pair<std::string, decltype(text)>{#text, text}</span>
- <span class="hljs-keyword" style="box-sizing: border-box;">template</span><<span class="hljs-keyword" style="box-sizing: border-box;">typename</span> T>
- <span class="hljs-keyword" style="box-sizing: border-box;">constexpr</span> <span class="hljs-keyword" style="box-sizing: border-box;">static</span> <span class="hljs-keyword" style="box-sizing: border-box;">inline</span> <span class="hljs-keyword" style="box-sizing: border-box;">auto</span> apply(T <span class="hljs-keyword" style="box-sizing: border-box;">const</span> & args)
- {
- <span class="hljs-keyword" style="box-sizing: border-box;">return</span> args;
- }
- <span class="hljs-keyword" style="box-sizing: border-box;">template</span><<span class="hljs-keyword" style="box-sizing: border-box;">typename</span> T, <span class="hljs-keyword" style="box-sizing: border-box;">typename</span> T1, <span class="hljs-keyword" style="box-sizing: border-box;">typename</span>... Args>
- <span class="hljs-keyword" style="box-sizing: border-box;">constexpr</span> <span class="hljs-keyword" style="box-sizing: border-box;">static</span> <span class="hljs-keyword" style="box-sizing: border-box;">inline</span> <span class="hljs-keyword" style="box-sizing: border-box;">auto</span> apply(T <span class="hljs-keyword" style="box-sizing: border-box;">const</span> & t, <span class="hljs-keyword" style="box-sizing: border-box;">const</span> T1& first, <span class="hljs-keyword" style="box-sizing: border-box;">const</span> Args&... args)
- {
- <span class="hljs-keyword" style="box-sizing: border-box;">return</span> apply(<span class="hljs-built_in" style="box-sizing: border-box;">std</span>::tuple_cat(t, <span class="hljs-built_in" style="box-sizing: border-box;">std</span>::make_tuple(MAKE_PAIR(first))), args...);
- }
- <span class="hljs-preprocessor" style="box-sizing: border-box;">#define META(...) auto meta(){ return apply(std::tuple<>(), __VA_ARGS__); }</span></font>
复制代码
使用现代C++之后,仅仅需要10行代码就可以实现之前需要上百行甚至数百行代码才能实现的目标,这无疑体现了现代C++的巨大威力。除了非常简洁的优点之外,还解决了一个宏元无法彻底解决的问题,宏元需要预先定义一些参数的宏,这些宏定义是有限的,如果参数超出定义的上限就会编译报错,而这个变参版本完全不用担心这个问题,支持任意个参数。 后记这件事给了我一个启示,如果我们一直按照前人的路去走,就很难超越前人,如果我们运用新的技术,改变思路,常常会化繁为简,化腐朽为神奇。只有通过新技术、新思维去创造、创新才能超越前人。 作者简介: 祁宇,珠海云创科技研发中心技术总监,《深入应用C++11》作者,C++开源社区purecpp.org创始人,致力于C++11的应用、研究和推广。乐于研究和分享技术,爱好C++,爱好开源。
|