开发者俱乐部

标题: 从一个例子看现代C++的威力 [打印本页]

作者: jack    时间: 2016-3-15 22:02
标题: 从一个例子看现代C++的威力
引子
最近准备重构一下我的kapok库,让meta函数可以返回元素为kv的tuple,例如:
  1. <font color="black"><span class="hljs-keyword" style="box-sizing: border-box;">struct</span> person
  2. {
  3.     <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;
  4.     <span class="hljs-keyword" style="box-sizing: border-box;">int</span> age;
  5.     META(name, age) <span class="hljs-comment" style="box-sizing: border-box;">//定义一个支持变参的meta函数</span>
  6. };
  7. <span class="hljs-keyword" style="box-sizing: border-box;">int</span> main()
  8. {
  9.     person p = {“tom”, <span class="hljs-number" style="box-sizing: border-box;">20</span>};
  10.     <span class="hljs-keyword" style="box-sizing: border-box;">auto</span> tp = p.meta();
  11.     <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”);
  12.     <span class="hljs-comment" style="box-sizing: border-box;">//tp在内存中是这样的 {{“name”:”tom”}, {“age”:20}};</span>
  13.     <span class="hljs-keyword" style="box-sizing: border-box;">return</span> <span class="hljs-number" style="box-sizing: border-box;">0</span>;
  14. }</font>
复制代码

类似这个META的实现我在msgpack的库里看到了,在这里:
仅仅是宏元的代码就数百行了,看起来非常复杂,msgpack之所以用这么复杂的方式去实现恐怕是为了支持c++98/03标准。本来想看看msgpack是如何实现META函数的,但是它的宏元代码读起来比较困难,于是作罢。
后来想起群里的ddrm实现了类似的功能,据说没有msgpack这么复杂,简洁一些,于是向ddrm要来了代码(在此感谢ddrm分享的源码)。他的代码也是用宏元,但是比msgpack的代码少很多,将近一百行代码。不过,我不太喜欢这么复杂的代码,我希望用一种更简单的方式去实现这个效果。这里附上ddrm的代码,大家可以借鉴参考一下。
  1. <font color="black"><span class="hljs-id" style="box-sizing: border-box;">#ifndef</span> TUPLE_MACRO_DEF_H
  2. <span class="hljs-id" style="box-sizing: border-box;">#define</span> TUPLE_MACRO_DEF_H

  3. <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
  4. <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

  5. <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)
  6. <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)
  7. <span class="hljs-hexcolor" style="box-sizing: border-box;">#def</span>ine PAIR_OBJECT(t) std::make_pair(#t, ADD_REFERENCE(t))
  8. <span class="hljs-hexcolor" style="box-sizing: border-box;">#def</span>ine PAIR_OBJECT_CONST(t) std::make_pair(#t, ADD_REFERENCE_CONST(t))
  9. <span class="hljs-hexcolor" style="box-sizing: border-box;">#def</span>ine MAKE_TUPLE(...) auto tuple() { return std::make_tuple(__VA_ARGS__);</span> }
  10. <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> }

  11. <span class="hljs-comment" style="box-sizing: border-box;">/* arg list expand macro, now support 40 args */</span>
  12. <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>
  13. <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>
  14. <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>
  15. <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>
  16. <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>
  17. <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>
  18. <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>
  19. <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>
  20. <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>
  21. <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>
  22. <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>
  23. <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>
  24. <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>
  25. <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>
  26. <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>
  27. <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>
  28. <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>
  29. <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>
  30. <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>
  31. <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>
  32. <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>
  33. <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>
  34. <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>
  35. <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>
  36. <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>
  37. <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>
  38. <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>
  39. <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>
  40. <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>
  41. <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>
  42. <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>
  43. <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>
  44. <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>
  45. <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>
  46. <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>
  47. <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>
  48. <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>
  49. <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>
  50. <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>
  51. <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>

  52. <span class="hljs-comment" style="box-sizing: border-box;">/* emmbed marco, using EMMBED_TUPLE(5 , a, b, c, d, e) */</span>
  53. <span class="hljs-comment" style="box-sizing: border-box;">//note use MACRO_CONCAT like A##_##B direct may cause marco expand error</span>
  54. <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>
  55. <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>

  56. <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> \
  57.         <span class="hljs-function" style="box-sizing: border-box;">MACRO_CONCAT(MAKE_ARG_LIST, N)</span>(op, arg, __VA_ARGS__)

  58. <span class="hljs-id" style="box-sizing: border-box;">#define</span> <span class="hljs-function" style="box-sizing: border-box;">EMMBED_TUPLE(N, ...)</span> \
  59. <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> \
  60. <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>

  61. <span class="hljs-comment" style="box-sizing: border-box;">// see  http://groups.google.com/group/comp.std.c/browse_thread/thread/77ee8c8f92e4a3fb/346fc464319b1ee5</span>
  62. <span class="hljs-id" style="box-sizing: border-box;">#define</span> <span class="hljs-function" style="box-sizing: border-box;">RSEQ_N()</span> \
  63.          63,62,61,60,                   \
  64.          59,58,57,56,55,54,53,52,51,50, \
  65.          49,48,47,46,45,44,43,42,41,40, \
  66.          39,38,37,36,35,34,33,32,31,30, \
  67.          29,28,27,26,25,24,23,22,21,20, \
  68.          19,18,17,16,15,14,13,12,11,10, \
  69.          9,8,7,6,5,4,3,2,1,0

  70. <span class="hljs-id" style="box-sizing: border-box;">#define</span> <span class="hljs-function" style="box-sizing: border-box;">ARG_N( \
  71.          _1, _2, _3, _4, _5, _6, _7, _8, _9,_10, \
  72.          _11,_12,_13,_14,_15,_16,_17,_18,_19,_20, \
  73.          _21,_22,_23,_24,_25,_26,_27,_28,_29,_30, \
  74.          _31,_32,_33,_34,_35,_36,_37,_38,_39,_40, \
  75.          _41,_42,_43,_44,_45,_46,_47,_48,_49,_50, \
  76.          _51,_52,_53,_54,_55,_56,_57,_58,_59,_60, \
  77.          _61,_62,_63,N, ...)</span> N

  78. <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>)
  79. <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>)

  80. <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>

  81. <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来说是不难办到的,下面是实现代码:
  1. <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>
  2. <span class="hljs-keyword" style="box-sizing: border-box;">template</span><<span class="hljs-keyword" style="box-sizing: border-box;">typename</span> T>
  3. <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)
  4. {
  5.     <span class="hljs-keyword" style="box-sizing: border-box;">return</span> args;
  6. }

  7. <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>
  8. <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)
  9. {
  10.     <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...);
  11. }

  12. <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++,爱好开源。







欢迎光临 开发者俱乐部 (http://xodn.com/) Powered by Discuz! X3.2