宏参数列表 [英] Macro parameter list

查看:95
本文介绍了宏参数列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的代码中有一个这样的宏。


#define DECLARE_SOMEWNDSTRUCTURE(style,nums,clrref,icon,tablenum,

name,

menuname)\

静态SOMEWNDSTRUCT& GetSomeWndStruct()\

{\

静态SOMEWNDSTRUCT somestruct = {\

style,bits,clrref,icon,tablenum,name,menuname}; \

返回somestruct; \

}
我希望能够做到这样的事情

有一个通用的参数列表...

GENERIC 1,2,3,4,5 ,6,7

GENERICNAME(名称)1,2,3,4,5,名称,7
然后可以这样使用。

DECLARE_SOMEWNDSTRUCTURE (GENERIC)

DECLARE_SOMEWNDSTRUCTURE(GENERICNAME(Jimbo))



显然这不起作用 - 警告C4003:实际不够

宏的参数...那么有人可以解释这样的事情是怎么做的吗?

解决方案



Jack Morgan写道:

我的代码中有一个像这样的宏。


#define DECLARE_SOMEWNDSTRUCTURE(style,nums,clrref,icon,tablenum,
name,
menuname)\
静态SOMEWNDSTRUCT& GetSomeWndStruct( )\
{静态SOMEWNDSTRUCT somestruct = {\
样式,位,clrref,图标,tablenum,名称,menuname}; \
返回somestruct; \\ \\ }


我希望能够做到这样的事情
有一个通用的参数列表...
GENERIC 1,2,3,4,5,6,7
GENERICNAME(名称)1,2,3,4,5,名称,7


然后可以这样使用。
DECLARE_SOMEWNDSTRUCTURE(GENERIC)
DECLARE_SOMEWNDSTRUCTURE(GENERICNAME(Jimbo))


显然这不是'不rk - 警告C4003:宏的实际
参数不够......那么有人可以解释这样的事情是怎么做的吗?




这里这是一个如何做到这一点的例子(有一些简单的测试代码;这个

已经过测试):


#define PRINT3D(a,b,c)do {printf("%d%d%d \ n,a,b,c);} while(0)

#define MACROCALL(x,y)x(y)

#define P3DARGS 1,2, 3

#include< stdio.h>


int main(无效)

{

MACROCALL(PRINT3D,P3DARGS);

返回0;

}


在许多预处理器问题中,额外的宏观扩张水平是所有需要的b $ b。这不应该太难以适应你的代码。


* Jack Morgan:

我有一个宏像这在我的代码中。
#define DECLARE_SOMEWNDSTRUCTURE(样式,nums,clrref,图标,tablenum,
名称,
menuname)\
静态SOMEWNDSTRUCT& GetSomeWndStruct()\
{\
静态SOMEWNDSTRUCT somestruct = {\
样式,位,clrref,图标,tablenum,名称,menuname}; \
返回somestruct; \
}
我希望能够做到这样的事情
有一个通用的参数列表...
GENERIC 1,2,3,4,5,6,7
GENERICNAME(名称)1,2,3,4,5,name,7
然后可以这样使用。
DECLARE_SOMEWNDSTRUCTURE(GENERIC)
DECLARE_SOMEWNDSTRUCTURE(GENERICNAME(" Jimbo") ;))
显然这不起作用 - 警告C4003:宏的实际
参数不够......所以有人可以解释这样的事情是怎么做的吗?


怎么样


#define DECLARE_NAMED(name)DECLARE_SOMEWNDSTRUCTURE(1,2,3,4,5,name,7)

#define DECLARE_DEFAULT DECLARE_NAMED(6)


但我猜这个宏观邪恶只是更多东西的症状

险恶。


从技术上讲,你试图发明默认的宏参数,以便

解决一些问题。很难看出那个问题是什么。但是

让我们说(纠正我,如果我错了)问题是


*如何为大多数值提供默认值一个集合。


一种方法是复制一个默认集合,只需更改不应该默认的值或值,例如


ValueCollection defaultValues(){...}

ValueCollection valuesWithName(std :: string const& aName)

{

ValueCollection结果= defaultValues();

result.name = aName;

返回结果;

}


或者它可能是虚拟函数的覆盖,比如


ValueCollection Base :: values()const {...}


std :: string Derived :: name()const {return" abra kadabra" ;; } $ / $

ValueCollection Derived :: values()const

{

ValueCollection result = Base :: values();

result.name = name();

返回结果;

}


你甚至可以去至于每个值使用一个虚函数。


-

答:因为它弄乱了人们通常阅读文本的顺序。 />
问:为什么这么糟糕?

A:热门发布。

问:usenet上最烦人的事情是什么?电子邮件?


您的代码生成错误C4003:没有足够的实际参数

宏''PRINT3D''


I got a macro like this in my code.

#define DECLARE_SOMEWNDSTRUCTURE(style, nums, clrref, icon, tablenum,
name,
menuname)\
static SOMEWNDSTRUCT &GetSomeWndStruct()\
{\
static SOMEWNDSTRUCT somestruct = {\
style, bits, clrref, icon, tablenum, name, menuname};\
return somestruct ;\
} And I want to be able to do something like this
Have a generic paremeter list...
GENERIC 1,2,3,4,5,6,7
GENERICNAME(name) 1,2,3,4,5,name,7 Which then could be used like this.
DECLARE_SOMEWNDSTRUCTURE(GENERIC)
DECLARE_SOMEWNDSTRUCTURE(GENERICNAME("Jimbo"))


Obviously this doesn''t work - warning C4003: not enough actual
parameters for macro...So could anybody explain how something like this
is done?

解决方案


Jack Morgan wrote:

I got a macro like this in my code.


#define DECLARE_SOMEWNDSTRUCTURE(style, nums, clrref, icon, tablenum,
name,
menuname)\
static SOMEWNDSTRUCT &GetSomeWndStruct()\
{\
static SOMEWNDSTRUCT somestruct = {\
style, bits, clrref, icon, tablenum, name, menuname};\
return somestruct ;\
}


And I want to be able to do something like this
Have a generic paremeter list...
GENERIC 1,2,3,4,5,6,7
GENERICNAME(name) 1,2,3,4,5,name,7


Which then could be used like this.
DECLARE_SOMEWNDSTRUCTURE(GENERIC)
DECLARE_SOMEWNDSTRUCTURE(GENERICNAME("Jimbo"))


Obviously this doesn''t work - warning C4003: not enough actual
parameters for macro...So could anybody explain how something like this
is done?



Here''s an example of how to do it (with some trivial test code; this
has been tested):

#define PRINT3D(a,b,c) do {printf("%d%d%d\n",a,b,c);} while(0)
#define MACROCALL(x,y) x(y)
#define P3DARGS 1,2,3

#include <stdio.h>

int main(void)
{
MACROCALL(PRINT3D,P3DARGS);
return 0;
}

In many preprocessor problems, an extra level of macro expansion is all
that is needed. This shouldn''t be too hard to adapt to your code.


* Jack Morgan:

I got a macro like this in my code.
#define DECLARE_SOMEWNDSTRUCTURE(style, nums, clrref, icon, tablenum,
name,
menuname)\
static SOMEWNDSTRUCT &GetSomeWndStruct()\
{\
static SOMEWNDSTRUCT somestruct = {\
style, bits, clrref, icon, tablenum, name, menuname};\
return somestruct ;\
}
And I want to be able to do something like this
Have a generic paremeter list...
GENERIC 1,2,3,4,5,6,7
GENERICNAME(name) 1,2,3,4,5,name,7
Which then could be used like this.
DECLARE_SOMEWNDSTRUCTURE(GENERIC)
DECLARE_SOMEWNDSTRUCTURE(GENERICNAME("Jimbo"))
Obviously this doesn''t work - warning C4003: not enough actual
parameters for macro...So could anybody explain how something like this
is done?



How about

#define DECLARE_NAMED(name) DECLARE_SOMEWNDSTRUCTURE(1,2,3,4,5,name,7)
#define DECLARE_DEFAULT DECLARE_NAMED( 6 )

But I guess this macro evilness is just a symptom of something more
sinister.

Technically you''re trying to invent default macro arguments in order to
solve some problem. It''s difficult to see what that problem is. But
let''s say (correct me if I''m wrong) that the problem is

* How to provide default values for most values in a collection.

And one way to do that is to copy a default collection and simply change
the value or values that should not default, e.g.

ValueCollection defaultValues() { ... }

ValueCollection valuesWithName( std::string const& aName )
{
ValueCollection result = defaultValues();
result.name = aName;
return result;
}

Or it could be an override of a virtual function, like

ValueCollection Base::values() const { ... }

std::string Derived::name() const { return "abra kadabra"; }

ValueCollection Derived::values() const
{
ValueCollection result = Base::values();
result.name = name();
return result;
}

You could even go so far as to use one virtual function per value.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?


Your code generates a error C4003: not enough actual parameters for
macro ''PRINT3D''


这篇关于宏参数列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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