在行业标准中的#define禁止? [英] Is #define banned in industry standards?

查看:140
本文介绍了在行业标准中的#define禁止?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是一年级计算机专业的学生,​​我的教授说的#define 在行业标准#如果 #IFDEF 的#else ,和其他几个preprocessor指令。他用因意外行为禁止这个词。

I am a first year computer science student and my professor said #define is banned in the industry standards along with #if, #ifdef, #else, and a few other preprocessor directives. He used the word "banned" because of unexpected behaviour.

这是准确的?如果是这样,为什么?

Is this accurate? If so why?

在那里,事实上,它禁止使用这些指令的标准是什么?

Are there, in fact, any standards which prohibit the use of these directives?

推荐答案

首先,我听说过。

没有; 的#define 等被广泛使用。有时过于广泛的应用,但绝对使用。有些地方的C标准要求使用宏 - 你不能避免那些容易。例如,第7.5节的错误<&errno.h中GT; 说:

No; #define and so on are widely used. Sometimes too widely used, but definitely used. There are places where the C standard mandates the use of macros — you can't avoid those easily. For example, §7.5 Errors <errno.h> says:

的宏

     EDOM
     EILSEQ
     ERANGE


  
  

这扩大到整型常量前pressions型 INT ,明显的正值,并适用于#如果<使用/ code> preprocessing指令; ...

which expand to integer constant expressions with type int, distinct positive values, and which are suitable for use in #if preprocessing directives; …

鉴于此,很显然,并非所有的行业标准禁止使用C preprocessor宏指令。不过,也有最佳实践或从美元使用C preprocessor的对$ pscribe限制,虽然没有完全禁止其使用​​的各种组织的编码准则的标准 - 这是C的一种与生俱来的部分,不能要完全避免。通常,这些标准对人安全关键领域的工作。

Given this, it is clear that not all industry standards prohibit the use of the C preprocessor macro directives. However, there are 'best practices' or 'coding guidelines' standards from various organizations that prescribe limits on the use of the C preprocessor, though none ban its use completely — it is an innate part of C and cannot be wholly avoided. Often, these standards are for people working in safety-critical areas.

一个标准,你可以查看 MISRA C (2012)标准;趋于禁止的事情,但即使承认的#define 等,有时需要(部分8.20,规则20.1通过20.14盖的C preprocessor)。

One standard you could check the MISRA C (2012) standard; that tends to proscribe things, but even that recognizes that #define et al are sometimes needed (section 8.20, rules 20.1 through 20.14 cover the C preprocessor).

美国航天局戈达德空间飞行中心(戈达德空间飞行中心)ç编码标准简单地说的:

The NASA GSFC (Goddard Space Flight Center) C Coding Standards simply say:

宏应使用只在必要时。宏过度使用可以使code难以阅读和维护,因为code不再读取或行为就像标准的C

Macros should be used only when necessary. Overuse of macros can make code harder to read and maintain because the code no longer reads or behaves like standard C.

这是介绍性发言后的讨论说明可接受的使用功能的宏。

The discussion after that introductory statement illustrates the acceptable use of function macros.

借助 CERT C编码标准有一些有关使用preprocessor的指导方针,并暗示你应该尽量少用preprocessor的,但并不禁止使用。

The CERT C Coding Standard has a number of guidelines about the use of the preprocessor, and implies that you should minimize the use of the preprocessor, but does not ban its use.

斯特劳斯想使preprocessor在C ++中无关紧要,但是这并没有发生。由于彼得 <一个href=\"https://stackoverflow.com/questions/34496418/is-define-banned-in-industry-standards/34496517?noredirect=1#comment56745967_34496517\">notes,一些C ++标准,如 JSF AV C ++编码标准联合攻击战斗机,飞行器的)从2005年大约,决定最小使用了C preprocessor的。本质上,JSF AV C ++规则,其限制为的#include 的#ifndef XYZ_H / 即$ p $#定义XYZ_H / ... / #ENDIF 舞蹈pvents一个头的多个包裹。 C ++有一些选项不可用的用C - 对,然后可以在地方C不会允许他们使用使用类型的常量值得注意的是,更好的支持。另请参见 静态常量 VS 的#define VS 枚举 获取的问题在那里。

Stroustrup would like to make the preprocessor irrelevant in C++, but that hasn't happened yet. As Peter notes, some C++ standards, such as the JSF AV C++ Coding Standards (Joint Strike Fighter, Air Vehicle) from circa 2005, dictate minimal use of the C preprocessor. Essentially, the JSF AV C++ rules restrict it to #include and the #ifndef XYZ_H / #define XYZ_H / … / #endif dance that prevents multiple inclusions of a single header. C++ has some options that are not available in C — notably, better support for typed constants that can then be used in places where C does not allow them to be used. See also static const vs #define vs enum for a discussion of the issues there.

这是一个好主意,尽量少用preprocessor ​​ - 它经常被滥用,至少尽可能多地使用(见的升压 preprocessor ​​'库'的多远,你可以用C preprocessor去)插图。

It is a good idea to minimize the use of the preprocessor — it is often abused at least as much as it is used (see the Boost preprocessor 'library' for illustrations of how far you can go with the C preprocessor).

在preprocessor是C的一个组成部分,的#define #如果等不能完全避免。在问题的教授的说法是不普遍有效的: 的#define 在行业标准#如果 #IFDEF 的#else ,和其他几个宏的是一个过度声明充其量不过可能是支持的,明确提及具体的行业标准(但有问题的标准,不包括ISO / IEC 9899:2011 - C标准)。

The preprocessor is an integral part of C and #define and #if etc cannot be wholly avoided. The statement by the professor in the question is not generally valid: #define is banned in the industry standards along with #if, #ifdef, #else, and a few other macros is an over-statement at best, but might be supportable with explicit reference to specific industry standards (but the standards in question do not include ISO/IEC 9899:2011 — the C standard).

注意大卫Hammen 具有的提供的信息的约一特定的C编码标准 - JPLç编码标准 - 禁止很多事情,很多人用C使用,其中包括限制使用中C preprocessor的(和限制使用动态内存分配,并禁止递归 - 读明白为什么,并决定这些原因是否与你有关的)。

Note that David Hammen has provided information about one specific C coding standard — the JPL C Coding Standard — that prohibits a lot of things that many people use in C, including limiting the use of of the C preprocessor (and limiting the use of dynamic memory allocation, and prohibiting recursion — read it to see why, and decide whether those reasons are relevant to you).

这篇关于在行业标准中的#define禁止?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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