使用前导下划线实际上会导致麻烦吗? [英] Does using leading underscores actually cause trouble?

查看:211
本文介绍了使用前导下划线实际上会导致麻烦吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

C / C ++标准保留以下划线标记的所有标识符(如果不在全局命名空间中则加上大写字母),或者包含两个或更多个相邻的下划线。示例:

The C/C++ standard reserves all identifiers that either lead with an underscore (plus an uppercase letter if not in the global namespace) or contain two or more adjacent underscores. Example:

int _myGlobal;
namespace _mine
{
    void Im__outta__control() {}
    int _LivingDangerously;
}

但是如果我只是不在乎呢?如果我决定危险地生活,并使用这些保留标识符怎么办?

But what if I just don't care? What if I decide to live dangerously and use these "reserved" identifiers anyway? Just how dangerously would I be living?

有没有看到由于用户代码使用保留的标识符而导致的编译器或链接器问题?

Have you ever actually seen a compiler or linker problem resulting from the use of reserved identifiers by user code?

下面的答案,到目前为止,相当于,为什么违反规则这样做可能会导致麻烦?但是想象一下,你已经有了一套破坏规则的代码。在什么时候违反规则的麻烦的代价大于重构代码遵从的代价?或者如果一个程序员开发了一个个人编码风格,呼吁野生下划线(例如可能来自另一种语言)?假设改变他们的编码风格对他们或多或少的痛苦,什么能激励他们克服痛苦?

The answers below, so far, amount to, "Why break the rules when doing so might cause trouble?" But imagine that you already had a body of code that broke the rules. At what point would the cost of trouble from breaking the rules outweigh the cost of refactoring the code to comply? Or what if a programmer had developed a personal coding style that called for wild underscores (perhaps by coming from another language, for instance)? Assuming that changing their coding style was more or less painful to them, what would motivate them to overcome the pain?

或者我可以反问同样的问题。具体是什么? C / C ++库正在使用用户可能违反的保留字?他们是否声明全局变量可能会创建名称冲突?功能?类?每个图书馆都是不同的,但是一般来说,这种冲突是如何表现的呢?

Or I could ask the same question in reverse. What is it concretely that C/C++ libraries are doing with reserved words that a user is liable to fall afoul of? Are they declaring globals that might create name clashes? Functions? Classes? Each library is different, naturally, but how in general might this collision manifest?

我教给软件学生们带着这些问题来找我,他们是,这违反了规则。这是一个迷信,挥手的答案。此外,在二十年的C ++编程中,我从来没有看到由于破坏保留字规则而导致的编译器或链接器错误。

I teach software students who come to me with these kinds of questions, and all I can tell them is, "It's against the rules." It's a superstitious, hand-waving answer. Moreover, in twenty years of C++ programming, I've never seen a compiler or linker error that resulted from breaking the reserved word rules.

一个好的怀疑者,面对任何规则,问:我为什么要关心?所以:为什么要关心?

A good skeptic, faced with any rule, asks, "Why should I care?" So: why should I care?

推荐答案

结果可能会根据您使用的特定编译器而有所不同。
关于危险级别 - 每次你会得到一个错误 - 你将不得不想知道它是源自你实现的逻辑还是从你不使用标准的事实。

The results may vary according to the specific complier you will use. Regarding the "danger level" - every time you'll get a bug - you will have to wonder if it is originates from your implemented logic or from the fact you are not using the standard.

但这不是全部...让我们假设有人告诉你:它是完全安全的!
所以,你可以做到这一点没有任何问题(只是假设..)
当你遇到一个bug或者你会想知道是否有一个轻微的chace他会重新定义你的想法错误? :)

But that is not all... let's assume someone tells you: "it is perfectly safe!" So, you can do that with no problem at all (only assuming..) Will it redefine your thinking when you get to a bug or still you will be wondering if there is a slight chace he was wrong? :)

所以,你看,无论你得到什么答案,它永远不会是一个好的。
(这让我真的喜欢你的问题)

So, you see, no matter which answer you will get it can never be a good one. (which makes me actually like your question)

这篇关于使用前导下划线实际上会导致麻烦吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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