如何用#define做不可能的事?>? [英] Howto do the impossible with #define ?>?

查看:45
本文介绍了如何用#define做不可能的事?>?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我有一个宏可以为其参数添加一个

间接等级,那肯定会很好。


所以如果我写:AddIndirection (X)


宏AddIndirection将执行:#define X(* X)


所以在此之后,无论何时使用X,它都会得到取而代之的是(* X)

现在我知道你不能让#define生成另一个#define,但是

有一些偷偷摸摸的方法吗?肯定那里的cpp大师可以

弄清楚怎么样?


谢谢,


grg

解决方案

Ancient_Hacker写道:


如果我有一个肯定会很好为其参数添加一个

间接级别的宏。


所以如果我写:AddIndirection(X)


宏AddIndirection将执行:#define X(* X)


所以在此之后,无论何时使用X,它都会被(* X)


您认为这将是不错的原因逃避我。

你打算对这个愚蠢的想法有什么用处?


现在我知道你不能拥有# define生成另一个#define,但

有一些偷偷摸摸的方法吗?肯定那里的cpp大师可以用b $ b来弄清楚怎么样?



是的,你可以做到。但是你需要用一点点来拼写

指令:而不是


AddIndirection(X)


你必须写好


#define X(* X)


....我怀疑你会很快后悔写作它。


-

Eric Sosman
es ***** @ acm-dot-org.inva lid




Eric Sosman写道:


您认为这将是不错的原因逃避我。

你打算用这种喋喋不休的想法有什么用处?



我是否对此问题提出了意见?


关于这个问题,它实际上是有效使用。


假设您正在编写一个动态数组库(用于C)。你想要用户能够写下来的



MakeMeAnArrayWithFourDimensions(float,MyArray,10,20,30,40)

不,您无需评论我选择的宏名称。


可以通过以下方式实现:


#define(t,n,d1,d2,d3,d4)\

typedef tn ## _ Type [d1] [d2] [d3] [d4]; \

typedef n ## _ Type * n ## _ Type_Ptr; \

static n ## _ Type_Ptr n; \

n =(n ## _ Type_Ptr)malloc(sizeof(n ## _ Type));

不,我不需要你对我的超级类型的评论定义和

多么聪明你可以把它压缩到一行。

现在唯一的问题是这个数组MyArray实际上是一个指向

的指针MyArray,所以可怜的用户必须写(* A)[i] [j] [k] [q],这对于
来说很烦人。或者我们可以提供另一个宏:#define

Access(n,i,j,k,q)(* n)[i] [j] [k] [q]但这也是低于

最佳。或者我们可以创建一个函数:tn(int d1,int d2,int

d3,int d4){

return * N ## _ Var [d1] [d2 ] [d3] [d4]} ....除了我们需要一个单独的

一个用于返回左值。


我能做的唯一方法看看如何使用尽可能透明

是做一个#define MyArray(* MyArray)。对于那些认为这是递归的人,它不是,cpp处理这种情况就好了。

我征求任何聪明的想法如何做到这一点或类似的。


谢谢,


grg


是的,你可以做到这一点。但是你需要用一点点来拼写

指令:而不是


AddIndirection(X)


你必须写好


#define X(* X)


......我怀疑你很快会后悔写它。



我想知道你为什么提这个。我的原始帖子清楚地表明

我希望自动执行此操作。你不知何故觉得有必要问这个问题,还有一个无可挑剔的能力,可以在

的未来中读懂我的想法。当然,有些神圣的力量有更好的事情要做,而不是在usenet新闻组上狡辩吗?


Ancient_Hacker写道:


Eric Sosman写道:


>你认为这将是好的原因逃避我。
你打算对这种愚蠢的想法有什么用处?



我是否要求就此问题提出意见?



不,你没问。你只是说确定

会很好,没有理由为什么有人会这样认为

如此。我质疑 - 并继续质疑 - 你用b $ b显然把它作为一个公理。


至于这个的blecheosity,它实际上有一个有效的用途。


假设您正在编写一个动态数组库(用于C)。你想要用户能够写下来的



MakeMeAnArrayWithFourDimensions(float,MyArray,10,20,30,40)

不,您无需评论我选择的宏名称。


可以通过以下方式实现:


#define(t,n,d1,d2,d3,d4)\

typedef tn ## _ Type [d1] [d2] [d3] [d4]; \

typedef n ## _ Type * n ## _ Type_Ptr; \

static n ## _ Type_Ptr n; \

n =(n ## _ Type_Ptr)malloc(sizeof(n ## _ Type));


不,我不需要你的评论关于我的超级类型定义和

你有多聪明可以把它压缩到一行。


现在唯一的问题是这个数组MyArray实际上是指向

MyArray,所以可怜的用户必须写(* A)[i] [j] [k] [q],这对于
很烦人。或者我们可以提供另一个宏:#define

Access(n,i,j,k,q)(* n)[i] [j] [k] [q]但这也是低于

最佳。或者我们可以创建一个函数:tn(int d1,int d2,int

d3,int d4){

return * N ## _ Var [d1] [d2 ] [d3] [d4]} ....除了我们需要一个单独的

一个用于返回左值。


我能做的唯一方法看看如何使用尽可能透明

是做一个#define MyArray(* MyArray)。对于那些认为

的人来说这是递归的,它不是,cpp处理这种情况就好了。



第二点优先:当然不是递归的;这是定义C语言的

方式。


至于更大的问题,你的问题是由错误引起的

选择指针类型。使用指向3D数组的指针来访问你的4D数组
,所有问题都将消失。请记住

C真的没有多维数组:它有一个 -
维数组,其元素可以是一维数组,
其元素又可以是一维数组,...

当你想要分配一个'char''的一维数组时,

说,你用什么指针类型? 零维是指零维。 `char *'',

不是一维`(char [N])*'',对吧?你不希望一个指针

到数组,你想要一个指向数组元素的指针 - 因此,你需要一个带有少一维指针的
;比整个阵列。


我征求任何关于如何做到这一点或类似的聪明想法。



如果通过this你的意思是导致自动重新定义,我想

你运气不好。如果是这个,你的意思是你要解决

问题,没有什么特别的聪明。关于上面概述的直截了当的解决方案,但它会更好用。


>>你必须写一下

#define X(* X)

......我怀疑你会很快后悔写作。



我想知道你为什么提这个。我的原始帖子清楚地表明

我希望自动执行此操作。你不知何故觉得有必要问这个问题,还有一个无可挑剔的能力,可以在

的未来中读懂我的想法。当然,有些神圣的力量有更好的事情要做,而不是在usenet新闻组上狡辩吗?



我提到我怀疑是否反对你不支持的断言

肯定会很好。我仍然怀疑。


-

Eric Sosman
es ***** @ acm-dot-org.inva 盖子


It sure would be nice if I could have a macro that add a level of
indirection to its argument.

So if I write: AddIndirection( X )

The macro AddIndirection will do: #define X (*X)

so after that point whenever you use X it gets replaced by (*X)
Now I KNOW you can''t have a #define generate another #define, but is
there some sneaky way to do this? Surely the cpp gurus out there can
figure out how?

Thanks,

grg

解决方案

Ancient_Hacker wrote:

It sure would be nice if I could have a macro that add a level of
indirection to its argument.

So if I write: AddIndirection( X )

The macro AddIndirection will do: #define X (*X)

so after that point whenever you use X it gets replaced by (*X)

The reason you think this would be "nice" escapes me.
What sort of use would you intend for this bletcherous idea?

Now I KNOW you can''t have a #define generate another #define, but is
there some sneaky way to do this? Surely the cpp gurus out there can
figure out how?

Yes, you can do it. But you''ll need to spell the
directive a little differently: instead of

AddIndirection(X)

you must write

#define X (*X)

.... and I suspect you will quickly come to regret writing it.

--
Eric Sosman
es*****@acm-dot-org.invalid



Eric Sosman wrote:

The reason you think this would be "nice" escapes me.
What sort of use would you intend for this bletcherous idea?


Did I ask for an opinion as to the nicety of this?

As to the blecherosity of this, it actually has a valid use.

Let''s say you''re writing a dynamic array library ( for C ). You''d like
the user to be able to write:

MakeMeAnArrayWithFourDimensions( float, MyArray, 10, 20, 30, 40 )

No, you need not comment on my choice of macro names.

One can implement this with:

#define(t,n,d1,d2,d3,d4) \
typedef t n##_Type [d1][d2][d3][d4]; \
typedef n##_Type * n##_Type_Ptr; \
static n##_Type_Ptr n; \
n = (n##_Type_Ptr) malloc( sizeof( n##_Type ) );
No, I don''t need your comments about my superflous type definitions and
how clever you could squeeze this down to one line.
Now the only problem is this array MyArray is actually a pointer to
MyArray, so the poor user will have to write (*A) [i][j][k][q], which
is annoying. Or we could supply yet another macro : #define
Access(n,i,j,k,q) (*n)[i][j][k][q] but that also is less than
optimum. or we could have created a function: t n(int d1, int d2, int
d3, int d4) {
return * N##_Var[d1][d2][d3][d4] } .... except we''d need a separate
one for returning lvalues.

The only way I can see how to make the use as transparent as possible
is to do a #define MyArray(*MyArray). For those of you that think
this is recursive, it''s not, cpp handles this case just fine.
I solicit any clever ideas as to how to do this or similar.

Thanks,

grg

Yes, you can do it. But you''ll need to spell the
directive a little differently: instead of

AddIndirection(X)

you must write

#define X (*X)

... and I suspect you will quickly come to regret writing it.


I wonder why you mention this. My original posting clearly indicates
my wish to do this automatically. You somehow feel compelled to beg
the question, plus show an unerring ability to read my mind in the
future. Surely someone of your Godlike powers has better things to do
than quibble on usenet newsgroups?


Ancient_Hacker wrote:

Eric Sosman wrote:

> The reason you think this would be "nice" escapes me.
What sort of use would you intend for this bletcherous idea?


Did I ask for an opinion as to the nicety of this?

No, you did not ask. You merely stated that "It sure
would be nice," offering no reason why anyone would think
so. I questioned -- and continue to question -- what you
apparently take as an axiom.

As to the blecherosity of this, it actually has a valid use.

Let''s say you''re writing a dynamic array library ( for C ). You''d like
the user to be able to write:

MakeMeAnArrayWithFourDimensions( float, MyArray, 10, 20, 30, 40 )

No, you need not comment on my choice of macro names.

One can implement this with:

#define(t,n,d1,d2,d3,d4) \
typedef t n##_Type [d1][d2][d3][d4]; \
typedef n##_Type * n##_Type_Ptr; \
static n##_Type_Ptr n; \
n = (n##_Type_Ptr) malloc( sizeof( n##_Type ) );
No, I don''t need your comments about my superflous type definitions and
how clever you could squeeze this down to one line.
Now the only problem is this array MyArray is actually a pointer to
MyArray, so the poor user will have to write (*A) [i][j][k][q], which
is annoying. Or we could supply yet another macro : #define
Access(n,i,j,k,q) (*n)[i][j][k][q] but that also is less than
optimum. or we could have created a function: t n(int d1, int d2, int
d3, int d4) {
return * N##_Var[d1][d2][d3][d4] } .... except we''d need a separate
one for returning lvalues.

The only way I can see how to make the use as transparent as possible
is to do a #define MyArray(*MyArray). For those of you that think
this is recursive, it''s not, cpp handles this case just fine.

Second point first: Of course it isn''t recursive; that''s the
way the C language is defined.

As for the larger issue, your problem is caused by the wrong
choice of pointer type. Use a pointer to a 3D array to access
your 4D array, and all the problems will disappear. Keep in mind
that C really doesn''t have multi-dimensional arrays: it has one-
dimensional arrays whose elements can be one-dimensional arrays,
whose elements can in turn be one-dimensional arrays, ...

When you want to allocate a one-dimensional array of `char'',
say, what pointer type do you use? A "zero-dimensional" `char*'',
not a one-dimensional `(char[N])*'', right? You don''t want a pointer
to the array, you want a pointer to the array element -- hence, you
want a pointer with "one less dimension" than the overall array.

I solicit any clever ideas as to how to do this or similar.

If by "this" you mean cause an automatic redefinition, I think
you are out of luck. If by "this" you mean you want to solve the
problem, there''s nothing especially "clever" about the straightforward
solution outlined above, but it will work lots better.

>>you must write

#define X (*X)

... and I suspect you will quickly come to regret writing it.


I wonder why you mention this. My original posting clearly indicates
my wish to do this automatically. You somehow feel compelled to beg
the question, plus show an unerring ability to read my mind in the
future. Surely someone of your Godlike powers has better things to do
than quibble on usenet newsgroups?

I mention my suspicion to counter your unsupported assertion
that "It sure would be nice." I remain suspicious.

--
Eric Sosman
es*****@acm-dot-org.invalid


这篇关于如何用#define做不可能的事?>?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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