c/c++语言开发共享令牌贴在C中

看完VA_NARG后

我尝试使用宏根据C中的参数数量实现函数重载。 现在的问题是:

void hello1(char *s) { ... } void hello2(char *s, char *t) { ... } // PP_NARG(...) macro returns number of arguments :ref to link above // does not work #define hello(...) hello ## PP_NARG(__VA_ARGS__) int main(void) { hello("hi"); // call hello1("hi"); hello("foo","bar"); // call hello2("foo","bar"); return 0; } 

我从C-faq那里读到了这个 。 但仍然无法让它工作……

    这是因为宏的评估规则。 您必须定义某种辅助宏,它将数字作为标记接收:

     #define HELLO_1(N, ...) hello ## N #define HELLO_0(N, ...) HELLO_1(N, __VARGS__) #define HELLO(...) HELLO_0(PP_NARG(__VA_ARGS__), __VARGS__) 

    或者。 您还可以浏览一下P99文档的预发行版。 这将为您提供更直接的宏工具。

    PP_NARG是一个相当令人印象深刻的疯狂!

    遵循C99标准(6.10.3.5,示例4)中的glue示例,以下结果产生了预期结果:

     #define glue(a, b) a ## b #define xglue(a, b) glue(a, b) #define hello(...) xglue(hello, PP_NARG(__VA_ARGS__))(__VA_ARGS__) 

    我没有可用于检查的C99编译器,但这应该有效:

     #define helloN(N, ...) hello ## N (__VA_ARGS__) #define hello(...) helloN(PP_NARG(__VA_ARGS__), __VA_ARGS__) 

      以上就是c/c++开发分享令牌贴在C中相关内容,想了解更多C/C++开发(异常处理)及C/C++游戏开发关注计算机技术网(www.ctvol.com)!)。

      本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。

      ctvol管理联系方式QQ:251552304

      本文章地址:https://www.ctvol.com/c-cdevelopment/560487.html

      (0)
      上一篇 2021年1月28日
      下一篇 2021年1月28日

      精彩推荐