c - 在宏中输出注释

标签 c c-preprocessor

我想要一组宏来声明这样的东西:

#define DECL_ITEM( var_name, type, array, flags, comment )  \
        type    var_name array,     ///< comment

不幸的是,预处理器会去掉 ///< comment . 有什么技巧可以让我的宏输出变量声明及其注释吗?

我希望如此

DECL_ITEM( var1, int, [ 10 ], 0, "What var1 stands for." )

输出如下:

int var1[ 10 ], ///< What var1 stands for.

谢谢!

最佳答案

我理解你的想法,但建议你使用像 PHP 这样的脚本语言作为你的代码生成器而不是 CPP。

一个例子是:

class   MetaInfo
{
    public $name;
    public $type;
    public $arr_w;
    public $flags;
    public $comment;

    public function __construct( $n, $t, $a, $f, $c )
    {
        $this->name     = $n;
        $this->type     = $t;
        $this->arr_w    = $a;
        $this->flags    = $f;
        $this->comment  = $c;
    }
};

function decl_db( $db_defs )
{
echo '
struct dataBase
{
';
    foreach( $db_defs as $def )
    {
        if ( $def->arr_w == "" )
            $decl="\t$def->type $def->name;             ///< $def->comment\n";
        else
            $decl="\t$def->type $def->name[ $def->arr_w ];      ///< $def->comment\n";
        print $decl;
    }
echo '
};
';
}
// ------------------------------------------------------------
// Custom DB definitions.

$db_defs = array(
    new MetaInfo( "var1",   "int",  "10",   "0",    "What var1 stands for." ),
);


decl_db( $db_defs );

它应该输出:

struct dataBase
{
    int var1[ 10 ], ///< What var1 stands for.
};

关于c - 在宏中输出注释,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10235377/

相关文章:

c - 在 Windows 上使用 asprintf()

c - 使用 pthreads 在结构中传递函数指针

objective-c - 从包含宏名称的 NSString 获取宏值

c++ - __FILE__、__LINE__ 和 __FUNCTION__ 在 C++ 中的用法

random - 使用 C 预处理器生成随机数

C 库的 C++ 包装器作为共享库

C - Malloc 和 memcpy(内存管理)

c - 为什么(带符号的)强制转换有时只需要表示最小整数(32 位系统)?

c - 定义扩展为可变数量元素的 C 宏

objective-c - Xcode 中 #elseifdef 的预处理指令无效