[MEC++]这样一个宏该怎么写?

Templater 2003-06-20 08:55:16
《More Exceptional C++》的Item 34中,Sutter提到了宏的以下用法,

可是他说的这个 ERR_ENTRY 宏该怎么写呢?多谢。

c) Variant Data Representations
A common example is that a module may define a list of error codes, which
outside users should see as a simple enum with comments, but which inside
the module should be stored in a map for easy lookup. That is:

// For outsiders
//
enum Error
{
ERR_OK = 0, // No error
ERR_INVALID_PARAM = 1, // <description>
...
};


// For the module's internal use
//
map<Error,const char*> lookup;
lookup.insert( make_pair( ERR_OK,
(const char*)"No error" ) );
lookup.insert( make_pair( ERR_INVALID_PARAM,
(const char*)"<description>" ) );
...

We'd like to have both representations, without defining the actual
information (code/message pairs) twice. With macro magic, we can simply
write a list of errors as follows, creating the appropriate structure
at compile time:

ERR_ENTRY( ERR_OK, 0, "No error" ),
ERR_ENTRY( ERR_INVALID_PARAM, 1, "<description>" ),
...

The implementations of ERR_ENTRY and related macros is left to the reader.
...全文
73 回复 打赏 收藏 转发到动态 举报
写回复
用AI写文章
回复
切换为时间正序
请发表友善的回复…
发表回复

70,035

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧