C++ 您遇到过的最糟糕的现实世界宏/预处理器滥用是什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/652788/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 16:31:16  来源:igfitidea点击:

What is the worst real-world macros/pre-processor abuse you've ever come across?

c++cmacrospreprocessor

提问by Trevor Boyd Smith

What is the worstreal-worldmacros/pre-processor abuse you've ever come across (please no contrived IOCCC answers *haha*)?

您遇到过的最糟糕的现实世界宏/预处理器滥用是什么(请不要人为的 IOCCC 答案*哈哈*)?

Please add a short snippet or story if it is really entertaining. The goal is to teach something instead of always telling people "never use macros".

如果真的很有趣,请添加一个简短的片段或故事。目标是教一些东西,而不是总是告诉人们“永远不要使用宏”。



p.s.: I've used macros before... but usually I get rid of them eventually when I have a "real" solution (even if the real solution is inlined so it becomes similar to a macro).

ps:我以前使用过宏......但通常当我有一个“真正的”解决方案时我最终会摆脱它们(即使真正的解决方案是内联的,所以它变得类似于宏)。



Bonus:Give an example where the macro was really was better than a not-macro solution.

奖励:举一个例子,其中宏确实比非宏解决方案更好。

Related question:When are C++ macros beneficial?

相关问题:C++ 宏何时有益?

回答by

From memory, it looked something like this:

从记忆中,它看起来像这样:

#define RETURN(result) return (result);}

int myfunction1(args) {
    int x = 0;
    // do something
    RETURN(x)

int myfunction2(args) {
    int y = 0;
    // do something
    RETURN(y)

int myfunction3(args) {
    int z = 0;
    // do something
    RETURN(z)

Yes that's right, no closing braces in any of the functions. Syntax highlighting was a mess, so he used vi to edit (not vim, it has syntax coloring!)

是的,没错,任何函数中都没有右大括号。语法高亮是一团糟,所以他用 vi 来编辑(不是 vim,它有语法着色!)

He was a Russian programmer who had mostly worked in assembly language. He was fanatical about saving as many bytes as possible because he had previously worked on systems with very limited memory. "It was for satellite. Only very few byte, so we use each byte over for many things." (bit fiddling, reusing machine instruction bytes for their numeric values) When I tried to find out what kinds of satellites, I was only able to get "Orbiting satellite. For making to orbit."

他是一名俄罗斯程序员,主要从事汇编语言工作。他热衷于尽可能多地保存字节,因为他以前曾在内存非常有限的系统上工作。“它是用于卫星的。只有很少的字节,所以我们将每个字节用于很多事情。” (摆弄,重复使用机器指令字节作为它们的数值)当我试图找出卫星的类型时,我只能得到“轨道卫星。为了进入轨道”。

He had two other quirks: A convex mirror mounted above his monitor "For knowing who is watching", and an occasional sudden exit from his chair to do a quick ten pushups. He explained this last one as "Compiler found error in code. This is punishment".

他还有另外两个怪癖:安装在显示器上方的凸面镜“因为知道谁在看”,以及偶尔突然从椅子上退出来快速做十个俯卧撑。他将最后一个解释为“编译器发现代码错误。这是惩罚”。

回答by i_am_jorf

My worst:

我最糟糕的:

#define InterlockedIncrement(x) (x)++
#define InterlockedDecrement(x) (x)--

I spent two days of my life tracking down some multi-threaded COM ref-counting issue because some idiot put this in a header file. I won't mention the company I worked for at the time.

我花了两天的时间来追踪一些多线程 COM 引用计数问题,因为有些白痴把它放在头文件中。我不会提到我当时工作的公司。

The moral of this story? If you don't understand something, read the documentation and learn about it. Don't just make it go away.

这个故事的寓意?如果您不理解某些内容,请阅读文档并了解它。不要只是让它消失。

回答by Joel Spolsky

#define ever (;;)
for ever { 
   ...
}

回答by Joel Spolsky

#include <iostream>
#define System S s;s
#define public
#define static
#define void int
#define main(x) main()
struct F{void println(char* s){std::cout << s << std::endl;}};
struct S{F out;};

public static void main(String[] args) {
  System.out.println("Hello World!");
}

Challenge: Can anyone do it with fewer defines and structs? ;-)

挑战:任何人都可以用更少的定义和结构来做到这一点吗?;-)

回答by Andy White

#define private public

回答by Michael McCarty

#define if while

It was joke played on someone, it wasn't found amusing by those affected

这是对某人开的玩笑,受影响的人并不觉得有趣

回答by paxdiablo

The hideous:

丑陋的:

#define begin {
#define end }
/* and so on */

Seriously, if you want to code in Pascal, buy a Pascal compiler, don't destroy the beautiful C language.

说真的,如果你想用 Pascal 写代码,买个 Pascal 编译器,别毁了漂亮的 C 语言。

回答by dcw

An 'architect', very humble guy, you know the type, had the following:

一个“建筑师”,非常谦虚的人,你知道的类型,有以下几点:

#define retrun return

because he liked to type fast. The brain-surgeon used to like to shout at people who were smarter than him (which was pretty much everyone), and threaten to use his black-belt on them.

因为他喜欢打字快。脑外科医生曾经喜欢对比他聪明的人(几乎每个人)大吼大叫,并威胁要对他们系上黑带。

回答by xtofl

Real-world? MSVC has macros in minmax.h, called maxand min, which cause a compiler error every time I intend to use the standard std::numeric_limits<T>::max()function.

真实世界?MSVC 在 minmax.h 中有宏,称为maxand min,每次我打算使用标准std::numeric_limits<T>::max()函数时都会导致编译器错误。

回答by mouviciel

A mix between Pascal syntax and french keywords:

Pascal 语法和法语关键字的混合:

#define debut {
#define fin }
#define si if(
#define alors ){
#define sinon }else{
#define finsi }