宏和 Visual C++

发布于 2024-07-14 17:38:03 字数 1265 浏览 4 评论 0原文

我试图更好地了解宏在现代 C++ 和 Visual C++ 中的位置(如果有),同时参考 Windows 编程库:宏在解决什么问题(如果有)这些情况如果不使用它们就无法解决?

我记得从 这篇博文,它们也用在 MFC 中 - 这是该博客文章中的宏示例,如果可能的话,我想详细解释一下:

// CWindowImpl
BEGIN_MSG_MAP(Edit)
  MSG_WM_CHAR(OnChar)
  MSG_WM_CONTEXTMENU(OnContextMenu)
  MSG_WM_COPY(OnCopy)
  MSG_WM_CUT(OnCut)
  MESSAGE_HANDLER_EX(WM_IME_COMPOSITION, OnImeComposition)
  MSG_WM_KEYDOWN(OnKeyDown)
  MSG_WM_LBUTTONDBLCLK(OnLButtonDblClk)
  MSG_WM_LBUTTONDOWN(OnLButtonDown)
  MSG_WM_LBUTTONUP(OnLButtonUp)
  MSG_WM_MBUTTONDOWN(OnNonLButtonDown)
  MSG_WM_MOUSEMOVE(OnMouseMove)
  MSG_WM_MOUSELEAVE(OnMouseLeave)
  MSG_WM_NCCALCSIZE(OnNCCalcSize)
  MSG_WM_NCPAINT(OnNCPaint)
  MSG_WM_RBUTTONDOWN(OnNonLButtonDown)
  MSG_WM_PASTE(OnPaste)
  MSG_WM_SYSCHAR(OnSysChar)  // WM_SYSxxx == WM_xxx with ALT down
  MSG_WM_SYSKEYDOWN(OnKeyDown)
END_MSG_MAP()

我已阅读 这些 MSDN 上与宏有关的文章,但我也尝试获取编写或避免宏以及在何处/何时使用宏的最佳实践他们。

I'm trying to get a better understanding of what place (if any) Macros have in modern C++ and Visual C++, also with reference to Windows programming libraries: What problem (if any) do Macros solve in these situations that cannot be solved without using them?

I remember reading about Google Chrome's use of WTL for Macros (amonst other things) from this blog post, and they are also used in MFC - here is an example of a Macro from that blog post I'd like explained in a superb amount of detail if possible:

// CWindowImpl
BEGIN_MSG_MAP(Edit)
  MSG_WM_CHAR(OnChar)
  MSG_WM_CONTEXTMENU(OnContextMenu)
  MSG_WM_COPY(OnCopy)
  MSG_WM_CUT(OnCut)
  MESSAGE_HANDLER_EX(WM_IME_COMPOSITION, OnImeComposition)
  MSG_WM_KEYDOWN(OnKeyDown)
  MSG_WM_LBUTTONDBLCLK(OnLButtonDblClk)
  MSG_WM_LBUTTONDOWN(OnLButtonDown)
  MSG_WM_LBUTTONUP(OnLButtonUp)
  MSG_WM_MBUTTONDOWN(OnNonLButtonDown)
  MSG_WM_MOUSEMOVE(OnMouseMove)
  MSG_WM_MOUSELEAVE(OnMouseLeave)
  MSG_WM_NCCALCSIZE(OnNCCalcSize)
  MSG_WM_NCPAINT(OnNCPaint)
  MSG_WM_RBUTTONDOWN(OnNonLButtonDown)
  MSG_WM_PASTE(OnPaste)
  MSG_WM_SYSCHAR(OnSysChar)  // WM_SYSxxx == WM_xxx with ALT down
  MSG_WM_SYSKEYDOWN(OnKeyDown)
END_MSG_MAP()

I've read these articles to do with Macros on MSDN but am trying to also pick up best practices for writing or avoiding Macros and where/when to use them.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

夜空下最亮的亮点 2024-07-21 17:38:03

所有这些宏都在公共 sdk 头文件中定义,因此您可以根据需要自行阅读它们的作用。 基本上,这里发生的是使用宏生成 WndProc 函数。 每个 MSG_WM_* 条目都是一个 case 语句,它通过将其参数从 wParam 和 lParam 转换为其原始类型并调用将这些类型作为参数的辅助函数(然后您可以实现)来处理给定的窗口消息(如果合适的话 -有些窗口消息有 0 或 1 个参数)。

在我看来,这一切都是垃圾。 您需要权衡必须预先编写一堆样板,但作为交换,您会使以后的调试变得更加困难。 宏在我的现代编程世界中没有太多地位,除了检查它们是否已定义之外。 进行流量控制的宏,或者假设总是定义特定的局部变量等,让我很不高兴。 特别是当我必须调试它们时。

All of those Macros are defined in public sdk header files, so you can go read what they do yourself if you want. Basically what is happening here is you're generating a WndProc function using Macros. Each MSG_WM_* entry is a case statement that handles the given window message by translating its arguments from wParam and lParam into their original types and calling a helper function (which you then get to go implement) that takes those types as arguments (if appropriate—some window messages have 0 or 1 arguments).

In my opinion all this is crap. You're trading off having to write a bunch of boiler-plate up front but in exchange you make it much harder to debug later. Macros don't have much of a place in my modern programming world, other than to check if they are defined or not. Macros that do flow control, or assume specific local variables are always defined, etc, make me quite unhappy. Especially when I have to debug them.

╰◇生如夏花灿烂 2024-07-21 17:38:03

Peter,你不妨问 vi 还是 EMACS 是更好的编辑器。 (顺便说一句,EMACS。)很多人认为 C 预处理器是一个可怕的想法;事实上,C 预处理器是一个非常糟糕的想法。 斯特鲁斯特鲁普和吉姆·高斯林就是其中之一。 这就是为什么 Java 没有预处理器,并且 Stroustrup 在 C++ 中放入了无数的东西,从 const 到模板,以避免使用预处理器。

还有其他人发现添加新语言很方便,就像在该代码中一样。

如果您阅读原始的 Bourne shell 代码,您会发现它看起来像

IF a == b THEN
   do_something();
   do_more();
ELSE
   do_less();
FI

Steve Bourne 基本上使用宏来赋予它类似 Algol68 的外观(当时这就是真正酷的语言。)对于任何人来说,使用它都可能非常困难,但史蒂夫·伯恩。

然后看一下混淆 C 竞赛,其中一些最令人惊奇的混淆利用了 #define

就我个人而言,我不太介意,尽管这个例子有点令人畏惧(你如何调试它?)但是做这种事情是在没有网络的情况下进行的; 那里没有多少瓦伦达。

Peter, you might as well ask if vi or EMACS is the better editor. (EMACS, by the way.) There are a lot of people who think the C preprocessor is a horrible idea; Stroustrup and Jim Gosling among them. That's why Java has no preprocessor, and there are innumerable things Stroustrup put into C++, from const to templates, to avoid using the preprocessor.

There are other people who find it convenient to be able to add a new language, as in that code.

If you read the original Bourne shell code, you'll find it looks like

IF a == b THEN
   do_something();
   do_more();
ELSE
   do_less();
FI

Steve Bourne basically used macros to give it an Algol68-like look (that being the Really Cool Language then.) It could be very difficult to work with for anyone but Steve Bourne.

Then have a look at the Obfuscated C contest, where some of the most amazing obfuscations take advantage of the #define.

Personally, I don't mind too much, although that example is a little daunting (how do you debug that?) But doing that kind of thing is working without a net; there aren't many Wallendas out there.

短暂陪伴 2024-07-21 17:38:03

(问题有点零散,但忽略了 Windows 部分):

X-Macros 使用预处理器以避免代码重复。

(The question is kind of fragmented, but ignoring the Windows part):

X-Macros use the preprocessor to avoid code duplication.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文