我想要一组宏来声明这样的东西:
#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/