如何生成此代码? [英] how to generate this code?

查看:54
本文介绍了如何生成此代码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,


我正在寻找这样的方式,工具或宏:


USEFULMACRO(foo) ,bar,type1 arg1,type2 arg2,...,typeN argN)


生成以下代码:


void foo(type1 arg1,type2 arg2,...,typeN argN)

{

bar(arg1,arg2,...,argN);

}


N是可变的。

函数foo调用bar使用相同的参数列表。

允许所有C ++类型(包括函数指针)。


有些想法吗?

解决方案

Roberto Gori写道:


我正在寻找一种方式,工具或宏这样的:


USEFULMACRO(foo,bar,type1 arg1,type2 arg2,...,typeN argN)


生成以下代码:


void foo(type1 arg1,type2 arg2,...,typeN argN)

{

bar(arg1,arg2 ,. ..,argN);

}


N是可变的。

函数foo调用bar使用相同的参数列表。

允许所有C ++类型(包括函数指针)。


有什么想法吗?



因为你必须在

宏的生成时将参数名称与它们的类型分开,你需要将它们分开在宏arg

列表:


USEFULMACRO(foo,bar,type1,arg1,...


否则type1 arg1是一个预处理令牌,并且没有

表示使用任何预处理器机制将其拆分为两个。


由于你需要一个可变数量的参数,你需要一个编译器

能够使用可变数量的参数的宏,否则你有

生成所有宏到最大数量args你的系统要处理

要处理。请参阅档案中的variadic macro。有些编译器可以这样做。\\ b
。它还不是标准的构造但是。


V

-

请在通过电子邮件回复时删除资金'A' br />
我没有回应top-p请回复,请不要问


Roberto Gori写道:


大家好,


我正在寻找一种方式,一种工具或像这样的宏:


USEFULMACRO(foo,bar,type1 arg1,type2 arg2, ...,键入N argN)


生成以下代码:


void foo(type1 arg1,type2 arg2,..., typeN argN)

{

bar(arg1,arg2,...,argN);

}


N是可变的。

函数foo调用bar使用相同的参数列表。

允许所有C ++类型(包括函数指针)。


有些想法吗?



无法完成。首先,在arg列表中间没有像

变量参数这样的概念。他们

必须在最后。其次,你可以用变量args做的唯一事情就是使用cstdarg

宏来提取它们。你不能将它们集体传递给另一个......

期待功能。


Ron Natalie写道:


Roberto Gori写道:


>大家好,

我正在寻找一个这样的工具或宏:

USEFULMACRO(foo,bar,type1 arg1,type2 arg2,...,typeN argN)

生成以下代码:

void foo(type1 arg1,type2 arg2,...,typeN argN)
{
bar(arg1,arg2,...,argN);
}

N是可变的。
函数foo调用bar使用相同的参数列表。
允许所有C ++类型(包括函数指针)。

一些想法?



无法完成。首先,在arg列表中间没有像

变量参数这样的概念。他们

必须在最后。其次,你可以用变量args做的唯一事情就是使用cstdarg

宏来提取它们。你不能将它们集体传递给另一个......

期待功能。



我不认为中间的变量参数是OP想要的。我也不认为大家超过他们是目标。让我们说我们

只有三个(或四个,取决于你如何计算)那里的东西:


USEFULMACRO(foo,bar,type arg )


它的定义是什么,因此可以生成这段代码:


void foo(类型arg){

bar(arg);

}





Methinks问题是试图提取第三个单词(令牌)" arg"
来自第三个宏观参数的



V

-

请在通过电子邮件回复时删除资金''A'

我没有回复最热门的回复,请不要问


Hi all,

i''m looking for a way, a tool or a macro like this:

USEFULMACRO(foo, bar, type1 arg1, type2 arg2, ..., typeN argN)

to generate the following code:

void foo(type1 arg1, type2 arg2, ..., typeN argN)
{
bar(arg1, arg2, ..., argN);
}

N is variable.
The function "foo" invokes "bar" with the same arguments list.
All the C++ types are admitted (function pointers included).

Some idea?

解决方案

Roberto Gori wrote:

i''m looking for a way, a tool or a macro like this:

USEFULMACRO(foo, bar, type1 arg1, type2 arg2, ..., typeN argN)

to generate the following code:

void foo(type1 arg1, type2 arg2, ..., typeN argN)
{
bar(arg1, arg2, ..., argN);
}

N is variable.
The function "foo" invokes "bar" with the same arguments list.
All the C++ types are admitted (function pointers included).

Some idea?

Since you have to separate names of arguments from their types in the
macro when it''s generated, you need to separate them in the macro arg
list:

USEFULMACRO(foo, bar, type1, arg1, ...

Otherwise "type1 arg1" is a single preprocessing token, and there are no
means to "split" it into two using any preprocessor mechanisms.

Since you want a variable number of arguments, you need a compiler
capable of macros with variable number of arguments, otherwise you have
to generate all macros up to maximum number of args your system is going
to handle. See "variadic macro" in the archives. Some compilers can do
that. It''s not a standard construct yet, though.

V
--
Please remove capital ''A''s when replying by e-mail
I do not respond to top-posted replies, please don''t ask


Roberto Gori wrote:

Hi all,

i''m looking for a way, a tool or a macro like this:

USEFULMACRO(foo, bar, type1 arg1, type2 arg2, ..., typeN argN)

to generate the following code:

void foo(type1 arg1, type2 arg2, ..., typeN argN)
{
bar(arg1, arg2, ..., argN);
}

N is variable.
The function "foo" invokes "bar" with the same arguments list.
All the C++ types are admitted (function pointers included).

Some idea?

Can''t be done. First, there is no such concept as
variable arguments in the middle of the arg list. They
must be at the end. Second, the only thing you can
do with variable args is extract them with the cstdarg
macros. You can''t pass them en masse to another ...
expecting function.


Ron Natalie wrote:

Roberto Gori wrote:

>Hi all,

i''m looking for a way, a tool or a macro like this:

USEFULMACRO(foo, bar, type1 arg1, type2 arg2, ..., typeN argN)

to generate the following code:

void foo(type1 arg1, type2 arg2, ..., typeN argN)
{
bar(arg1, arg2, ..., argN);
}

N is variable.
The function "foo" invokes "bar" with the same arguments list.
All the C++ types are admitted (function pointers included).

Some idea?

Can''t be done. First, there is no such concept as
variable arguments in the middle of the arg list. They
must be at the end. Second, the only thing you can
do with variable args is extract them with the cstdarg
macros. You can''t pass them en masse to another ...
expecting function.

I don''t think the variable arguments in the middle is what OP wants. I
also don''t think that passing them en masse is the goal. Let''s say we
only have three (or four, depending on how you count) things there:

USEFULMACRO(foo, bar, type arg)

What would be its definition so it is possible to generate this code:

void foo(type arg) {
bar(arg);
}

?

Methinks the problem is trying to extract the second word (token) "arg"
from the third macro argument.

V
--
Please remove capital ''A''s when replying by e-mail
I do not respond to top-posted replies, please don''t ask


这篇关于如何生成此代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆