热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

为什么c++语言的设计者一直使用关键字?-WhydotheC++languagedesignerskeepre-usingkeywords?

Whatisthemainargumentinfavorofre-usingshortkeywords(andaddingcontext-dependentmeanings

What is the main argument in favor of re-using short keywords (and adding context-dependent meanings) instead of just adding more keywords?

支持重用短关键字(并添加上下文相关的含义)而不是只添加更多关键字的主要论据是什么?

Is it just that you want to avoid breaking existing code that may already be using a proposed new keyword, or is there a deeper reason?

是否只是想避免破坏现有的代码,而这些代码可能已经在使用一个新的关键字,或者有更深层次的原因?

The new "enum class" in C++11 got me thinking about this, but this is a general language design question.

c++ 11中新的“enum类”让我想到了这个问题,但这是一个通用的语言设计问题。

5 个解决方案

#1


68  

Is it just that you want to avoid breaking existing code that may already be using a proposed new keyword, or is there a deeper reason?

是否只是想避免破坏现有的代码,而这些代码可能已经在使用一个新的关键字,或者有更深层次的原因?

No, that's the reason.

不,这是原因。

Keywords, by definition, are always considered keywords wherever they occur in the source, so they cannot be used for other purposes. Making something a keyword breaks any code that might be using that token as a variable, function, or type name.

根据定义,关键字在源文件中出现的任何地方都被认为是关键字,因此它们不能用于其他目的。将某项设置为关键字会破坏任何可能将该标记用作变量、函数或类型名称的代码。

The C committee take a different approach and add new keywords using _Reserved names, e.g. _Atomic, _Bool, and then they add a new header (, ) with a nicer macro, so that you can choose whether to include the header to get the name atomic or bool, but it won't be declared automatically and won't break code that happens to be using those names already.

C委员会采用不同的方法,使用_reserve名称添加新的关键字,例如_Atomic, _Bool,然后添加一个新的头( )具有更好的宏,因此您可以选择是否包含header以获得名称atomic或bool,但它不会自动声明,也不会破坏碰巧正在使用这些名称的代码。

The C++ committee don't like macros and want them to be proper keywords, so either re-use existing ones (such as auto) or add context-dependent "keywords" (which are not really keywords, but are "identifiers with special meaning" so they can be used for other things, such as override) or use strange spellings that are unlikely to clash with user code (such as decltype instead of the widely supported typeof extension).

c++委员会不喜欢宏和希望他们适当的关键字,所以要么重用现有的(例如汽车)或添加上下文相关的“关键字”(这并不是真正的关键字,但“标识符与特殊意义”,这样他们就可以被用于其他事情,如覆盖)或使用奇怪的拼写,不太可能与用户代码(比如decltype而不是广泛支持typeof的扩展)。

#2


17  

Some old languages did not have keywords at all, in particular PL/1 where

一些旧语言根本没有关键字,尤其是PL/1

IF IF=THEN THEN BEGIN;
  /* some more code */
END;

was a legal piece of code, but completely unreadable. (Look also into APL as an example of write-mostly programming language, which is completely cryptic to read a few months later, even by the code's original author).

是一段合法的代码,但完全不可读。(也可以将APL作为以写为主的编程语言的一个例子,即使是由代码的原始作者编写,在几个月后读它也是完全不清楚的)。

The C and C++ language family have a set of keywords defined by the language specification. But there are very widely used languages with billions of legacy source code lines. If you (or their standardization committee) add a new keyword, there is a chance of collisions with some existing program, and as you guessed and others answered this is bad. So if the standard added for instance enum_class as a new keyword, chances are that someone would already have used it as an identifier, and that entity would be unhappy (to have to change their code when adopting a new C++ standard).

C和c++语言家族有一组由语言规范定义的关键字。但是有非常广泛使用的语言,有数十亿的遗留源代码行。如果您(或者他们的标准化委员会)添加了一个新的关键字,那么就有可能与一些现有的程序发生冲突,正如您所猜测的那样,其他人的回答是不好的。因此,如果为instance enum_class添加了一个新的关键字,那么很有可能有人已经将它用作标识符,而这个实体会不高兴(在采用新的c++标准时,不得不更改其代码)。

Also C++ is widely known to be slowly parsed (in particular, because standard headers like are pulling dozen of thousand lines of source code, and because modules are not in C++ yet, and because the syntax is strongly ambiguous), so complexifying the parser to handle new syntax is not a big deal (parsing C++ has always been horrible anyway). For example the GCC community is working much harder on new optimizations than on new C++ features (apparently, recent features of the C++ standard library requires much work than parsing new syntax), even if the jump from C++03 to C++11 was a huge jump and required a lot of work in the C++ frontend. This is less true for the C++11 to C++14 jump.

c++也被广泛认为是慢慢解析(特别是,因为标准头喜欢 <向量> 拉打几千行源代码,因为模块还没有在c++中,因为语法是强烈模糊),因此复杂化的解析器来处理新语法不是一个大问题(解析c++一直是可怕的)。例如GCC社区是新优化工作更加困难比新的c++特性(显然,最近c++标准库的特点要求比解析新语法)工作,即使从c++ 03跳到c++ 11是一个巨大的跳跃,需要大量的在c++前台工作。对于c++ 11到c++ 14的跳转,情况就不太一样了。

Some other languages (e.g. some dialects of Lisp such as Common Lisp and some Scheme, where you could redefine a let or if macro, and macros in homoiconic languages like these are very different, since operating on ASTs, from the crude textual substitution mechanism in C or C++...) permit the redefinition of existing keywords; read also about hygienic macros. But this can make the source code difficult to understand a few months later.

一些其他语言(如Lisp的一些方言,如Common Lisp和一些计划,你可以重新定义一个让或者宏,在homoiconic和宏语言,像这些都是非常不同的,因为在ast操作,从原油文本替换机制在C或c++…)允许重新定义现有的关键词;还可以阅读有关健康宏的内容。但这可能会使源代码在几个月后难以理解。

#3


10  

I think it's mainly because adding keywords will break existing code that happens to use this keyword in other contexts, as you suggest.

我认为主要是因为添加关键字会破坏现有的代码,而这些代码恰好在其他上下文中使用了这个关键字,正如您所建议的那样。

#4


10  

Is it just that you want to avoid breaking existing code that may already be using a proposed new keyword, or is there a deeper reason?

是否只是想避免破坏现有的代码,而这些代码可能已经在使用一个新的关键字,或者有更深层次的原因?

By definition, a keyword is a special token which cannot be used anywhere else; as a result, introducing a keyword breaks any code that happened to use an identifier with the given spelling.

根据定义,关键字是一种特殊的令牌,在其他任何地方都不能使用;因此,引入关键字会破坏使用给定拼写的标识符的任何代码。

Some languages use the term contextual keyword to refer to spellings that are only interpreted as keyword in specific contexts. If no "wild" identifier could previously be used in this context, then it is guaranteed that the introduction of the contextual keyword will not break existing code. For example, since no identifier can appear immediately after the closing parenthese in a function signature, this is a place where one can introduce so-called contextual keywords (such as override or final).

有些语言使用“上下文关键字”一词来指代仅在特定上下文中被解释为关键字的拼写。如果之前不能在此上下文中使用“wild”标识符,则可以保证上下文关键字的引入不会破坏现有代码。例如,由于在函数签名的结束括号之后不能立即出现任何标识符,因此可以在这里引入所谓的上下文关键字(例如override或final)。

On the other hand, in places where any identifier was previously allowed, adding a keyword poses a risk. For example:

另一方面,在以前允许任何标识符的地方,添加关键字会带来风险。例如:

  • struct H { my_type f; enum { g }; };: the use of enum class rather than a new keyword is because any new word could be mistakenly taken as the start of a data member declaration in this context; only a keyword is unambiguous (in LL(1)), and introducing a new one could break code.
  • 结构H {my_type f;enum { g };};: enum类而不是new关键字的使用,是因为在此上下文中任何新单词都可能被错误地视为数据成员声明的开始;只有关键字是明确的(在LL(1)中),引入一个新的关键字可能会破坏代码。
  • void h() { my_type f; auto x = g(); }: the use of auto rather than a new keyword is because any new word could clash with an existing type. It's a surprising choice still, since it was already a keyword usable in this position in C (defaulting to int type) but its meaning was altered (the justification was the low probability of its usage).
  • 无效h() {my_type f;汽车x = g();}:使用auto而不是new关键字是因为任何新单词都可能与现有类型发生冲突。这是一个令人惊讶的选择,因为在C(默认为int类型)中,它已经是一个关键字,但是它的含义被改变了(理由是它使用的可能性很低)。

As some have mentioned, languages can be designed without keywords entirely (Haskell comes pretty close), or made in a way than keywords can be introduced seamlessly (for example, if every declaration starts by a keyword already, then introducing a new keyword cannot clash). It just so happens than C and C++ where not made so, and indeed many C-like languages.

正如有些人所提到的,语言可以完全不使用关键字来设计(Haskell非常接近),或者以一种关键字可以无缝引入的方式进行设计(例如,如果每个声明都以一个关键字开头,那么引入一个新的关键字就不会产生冲突)。它只发生在C和c++中,而不是这样,而且确实有许多C语言。

#5


-11  

Mistaken enthusiasm of "less is more". It is thought (incorrectly) that by using fewer keywords, programmers would have to learn less and can be more productive sooner. But this only creates confusion about the syntax.

“少即是多”的错误热情。人们认为(错误地)使用更少的关键字,程序员必须学习更少,并且可以更快地提高效率。但这只会造成语法上的混乱。

"Real Perl programmers prefer things to be visually distinct." ---- Larry Wall

“真正的Perl程序员更喜欢在视觉上区分事物。”——拉里墙

In other words, use a keyword for one task only.

换句话说,只对一个任务使用关键字。


推荐阅读
  • 1.如何在运行状态查看源代码?查看函数的源代码,我们通常会使用IDE来完成。比如在PyCharm中,你可以Ctrl+鼠标点击进入函数的源代码。那如果没有IDE呢?当我们想使用一个函 ... [详细]
  • 扫描线三巨头 hdu1928hdu 1255  hdu 1542 [POJ 1151]
    学习链接:http:blog.csdn.netlwt36articledetails48908031学习扫描线主要学习的是一种扫描的思想,后期可以求解很 ... [详细]
  • 本文介绍如何使用Objective-C结合dispatch库进行并发编程,以提高素数计数任务的效率。通过对比纯C代码与引入并发机制后的代码,展示dispatch库的强大功能。 ... [详细]
  • DNN Community 和 Professional 版本的主要差异
    本文详细解析了 DotNetNuke (DNN) 的两种主要版本:Community 和 Professional。通过对比两者的功能和附加组件,帮助用户选择最适合其需求的版本。 ... [详细]
  • 采用IKE方式建立IPsec安全隧道
    一、【组网和实验环境】按如上的接口ip先作配置,再作ipsec的相关配置,配置文本见文章最后本文实验采用的交换机是H3C模拟器,下载地址如 ... [详细]
  • 在编译BSP包过程中,遇到了一个与 'gets' 函数相关的编译错误。该问题通常发生在较新的编译环境中,由于 'gets' 函数已被弃用并视为安全漏洞。本文将详细介绍如何通过修改源代码和配置文件来解决这一问题。 ... [详细]
  • 本文详细介绍了虚拟专用网(Virtual Private Network, VPN)的概念及其通过公共网络(如互联网)构建临时且安全连接的技术特点。文章探讨了不同类型的隧道协议,包括第二层和第三层隧道协议,并提供了针对IPSec、GRE以及MPLS VPN的具体配置指导。 ... [详细]
  • 深入探讨栈和队列的应用实例——铁轨问题(Rails, ACM/ICPC CERC 1997, UVa 514)。该问题设定在一个城市火车站,涉及n节车厢从A方向驶入车站,并需按照特定顺序驶出B方向的铁轨。本文将通过算法实现来验证特定顺序的可行性。 ... [详细]
  • 本文探讨了一个在Mac Mavericks系统上使用Clang++成功编译但通过R CMD SHLIB构建并在R中加载时遇到‘符号未找到’错误的C++程序问题。文章详细分析了错误原因,并提供了有效的解决方案。 ... [详细]
  • 本文详细介绍了Java中org.eclipse.ui.forms.widgets.ExpandableComposite类的addExpansionListener()方法,并提供了多个实际代码示例,帮助开发者更好地理解和使用该方法。这些示例来源于多个知名开源项目,具有很高的参考价值。 ... [详细]
  • 使用 Azure Service Principal 和 Microsoft Graph API 获取 AAD 用户列表
    本文介绍了一段通用代码示例,该代码不仅能够操作 Azure Active Directory (AAD),还可以通过 Azure Service Principal 的授权访问和管理 Azure 订阅资源。Azure 的架构可以分为两个层级:AAD 和 Subscription。 ... [详细]
  • 本文详细介绍了Akka中的BackoffSupervisor机制,探讨其在处理持久化失败和Actor重启时的应用。通过具体示例,展示了如何配置和使用BackoffSupervisor以实现更细粒度的异常处理。 ... [详细]
  • 本文介绍了如何在C#中启动一个应用程序,并通过枚举窗口来获取其主窗口句柄。当使用Process类启动程序时,我们通常只能获得进程的句柄,而主窗口句柄可能为0。因此,我们需要使用API函数和回调机制来准确获取主窗口句柄。 ... [详细]
  • 本文介绍了一个C++程序,该程序用于计算一个向量首尾索引的和。当向量长度为偶数时,程序会遇到对称对,如v1[0] + v1[last]与v1[last] + v1[0],这些实际上是相同的计算结果,因此需要排除重复项以提高效率。 ... [详细]
  • 本文详细探讨了一道涉及算法、C++及图论知识点的题目,适合对算法竞赛感兴趣的读者。通过分析题目【这是一道大水题】,我们将探索如何高效地处理区间查询与更新问题。本文由技术作者【ღCauchyོꦿ࿐】撰写,旨在帮助读者掌握相关技术和解题技巧。 ... [详细]
author-avatar
日月星辰淡定鍀好男孩_933
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有