gpt4 book ai didi

c - 在宏中输出注释

转载 作者:太空宇宙 更新时间:2023-11-04 03:02:10 26 4
gpt4 key购买 nike

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

#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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com