c++ - Variadac 宏将宏应用于所有参数

标签 c++ c++11 c-preprocessor variadic-macros

我正在试验 C++11 variadac 宏。

我试图对列表中的每个参数应用另一个宏。
这是我的第一次尝试:

#define  APPLY_CHAIN(first, ...)    APPLY_ACT(first) APPLY_CHAIN( __VA_ARGS__ )

不幸的是,这没有用。

我最终让它工作了。但它有点复杂,并且有一个“n”的限制(其中“n”是我愿意为其键入宏的最大大小)。

#define COUNT_N(_1, _2, _3, _4, _5, _6, _7, _8, _9, _10, N, ...)    N
#define COUNT(...)   COUNT_N( __VA_ARGS__, 10, 9, 8, 7, 6, 5 ,4, 3, 2, 1)

#define BUILD_CHAIN_1(_1)       APPLY_ACT(_1)
#define BUILD_CHAIN_2(_1, ...)  APPLY_ACT(_1) BUILD_CHAIN_1(__VA_ARGS__)
#define BUILD_CHAIN_3(_1, ...)  APPLY_ACT(_1) BUILD_CHAIN_2(__VA_ARGS__)
#define BUILD_CHAIN_4(_1, ...)  APPLY_ACT(_1) BUILD_CHAIN_3(__VA_ARGS__)
#define BUILD_CHAIN_5(_1, ...)  APPLY_ACT(_1) BUILD_CHAIN_4(__VA_ARGS__)
#define BUILD_CHAIN_6(_1, ...)  APPLY_ACT(_1) BUILD_CHAIN_5(__VA_ARGS__)
#define BUILD_CHAIN_7(_1, ...)  APPLY_ACT(_1) BUILD_CHAIN_6(__VA_ARGS__)
#define BUILD_CHAIN_8(_1, ...)  APPLY_ACT(_1) BUILD_CHAIN_7(__VA_ARGS__)
#define BUILD_CHAIN_9(_1, ...)  APPLY_ACT(_1) BUILD_CHAIN_8(__VA_ARGS__)
#define BUILD_CHAIN_10(_1,...)  APPLY_ACT(_1) BUILD_CHAIN_9(__VA_ARGS__)


#define BUILD_CHAIN_INC( CT, ...)   BUILD_CHAIN_ ## CT (__VA_ARGS__)
#define BUILD_CHAIN( CT, ...)       BUILD_CHAIN_INC(CT, __VA_ARGS__)
#define APPLY_CHAIN(...)            BUILD_CHAIN( COUNT(__VA_ARGS__), __VA_ARGS__)

#define APPLY_ACT(X)                #X

#include <iostream>

int main()
{
    std::cout << APPLY_CHAIN(1,2,3,4,5) << "\n";
}

所以我的问题是:我是否遗漏了一些简单的东西,可以让我沿着参数链迭代

最佳答案

您对参数计数问题的解决方案与 Laurent Deniau 在 2006 年 1 月发布到 [comp.lang.c] (IIRC) 的解决方案相同。

修改后也可以与 Visual C++ 一起使用,他的参数计数如下所示:

#pragma once

/*
* The PP_NARG macro evaluates to the number of arguments that have been
* passed to it.
*
* Laurent Deniau, "__VA_NARG__," 17 January 2006, <comp.std.c> (29 November 2007).
* https://groups.google.com/forum/?fromgroups=#!topic/comp.std.c/d-6Mj5Lko_s
*/

// Added workaround for silly MSVC bug that yields "too few parameters" warning.
// - Alf
#include <progrock/cppx/macro/invoke.h>         // CPPX_INVOKE
#include <progrock/cppx/macro/concat.h>         // CPPX_CONCAT

#define PP_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,...) N

#define PP_RSEQ_N() \
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

#if 0
    #define PP_NARG_(...) PP_ARG_N( __VA_ARGS__ )
    #define PP_NARG( ...) PP_NARG_( __VA_ARGS__, PP_RSEQ_N() )
#else
    #define PP_NARG_(...) CPPX_INVOKE( PP_ARG_N, (__VA_ARGS__) )
    #define PP_NARG(...) PP_NARG_(__VA_ARGS__,PP_RSEQ_N())
#endif

CPPX_CONCAT 宏:

#pragma once

#define CPPX_CONCAT__( a, b )       a ## b
#define CPPX_CONCAT_( a, b )        CPPX_CONCAT__( a, b )
#define CPPX_CONCAT( a, b )         CPPX_CONCAT_( a, b )

CPPX_INVOKE 宏:

#pragma once

#define CPPX_INVOKE( macro, args ) macro args

示例用法:

#pragma once
#include <progrock/cppx/macro/nargs.h>          // CPPX_NARGS, CPPX_CONCAT

#define CPPX_IS_UNUSED_( name ) \
    (void) name; struct name

#define CPPX_IS_UNUSED_1( a ) \
    CPPX_IS_UNUSED_( a )

#define CPPX_IS_UNUSED_2( a, b ) \
    CPPX_IS_UNUSED_1( a ); CPPX_IS_UNUSED_( b )

#define CPPX_IS_UNUSED_3( a, b, c ) \
    CPPX_IS_UNUSED_2( a, b ); CPPX_IS_UNUSED_( c )

#define CPPX_IS_UNUSED_4( a, b, c, d ) \
    CPPX_IS_UNUSED_3( a, b, c ); CPPX_IS_UNUSED_( d )

#define CPPX_IS_UNUSED_5( a, b, c, d, e ) \
    CPPX_IS_UNUSED_4( a, b, c, d ); CPPX_IS_UNUSED_( e )

#define CPPX_IS_UNUSED( ... )      \
    CPPX_INVOKE( CPPX_CONCAT( CPPX_IS_UNUSED_, CPPX_NARGS( __VA_ARGS__ ) ), ( __VA_ARGS__ ) )

#define CPPX_IS_INTENTIONALLY_UNUSED    CPPX_IS_UNUSED

因为这说明了主要线索是不使用递归(至少不直接使用),而是为每个参数数量手动重复代码。

我相信递归宏在形式上是不可能的。然而,很少有编译器符合这一领域,至少几年前您确实可以哄骗 WIndows 编译器进行递归宏替换。我记得这是最初在最终成为 Boost 预处理器库的代码中使用的技术(现在它使用正式有效的代码,通过集中大量特定于参数的处理程序,除了特定于编译器的怪癖的解决方法) .

代码重复最有可能通过 Boost 预处理器库自动执行,但是你也可以将 Boost 用于其他东西(大概是为了避免依赖像 Boost 这样的大型库,至少那是我的动机当我写上面的内容时)。

免责声明:我在发布之前没有重试这段代码,我不记得它是否处于被修改的状态,或者是什么。但它确实显示了一般原则。

关于c++ - Variadac 宏将宏应用于所有参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15847837/

相关文章:

c++ - Lambda 函数模板转换失败

c++ - 在 C MBED 中使用 strstr,打印结果

c++ - 如何在 C++ 中创建一组无序的整数对?

c++ - 如何在旧版本的 gcc 系统上动态链接到 libc.so.6、libstdc++.so.6 的本地拷贝

c++ - 如何在应用程序退出()期间处理 Qthread 终止?

c++ - 使用 USERNAME :PASSWORD but not for SERIAL:TOKEN? 进行字符串化

c++ - 你如何保持神经网络训练完好无损?

c++ - 常量 reference_wrapper

c - 在 VS 调试器 : any way to hover and see values? 中使用#define

c - 自动编译所有 ifdef/ifndef 指令的工具