为什么不选择C ++ 14位数字分隔符的空格字符? [英] Why was the space character not chosen for C++14 digit separators?

查看:226
本文介绍了为什么不选择C ++ 14位数字分隔符的空格字符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

自C ++ 14起,感谢 n3781 (其本身不回答这个问题),我们可以编写如下代码:

  const int x = 1'234 ; //一千二百三十四

目的是改进这样的代码: p>

  const int y = 100000000; 

并使其更易读。



下划线( _ )已经在C ++ 11中由用户定义的文字引用,逗号()有本地化问题—许多欧洲国家令人费解的使用它作为小数分隔符—和逗号运算符冲突,虽然我不知道什么真实世界的代码可能已经打破了允许例如。 1,234,567



无论如何,一个更好的解决方案似乎是空格字符:

  const int z = 1 000 000; 

这些相邻的数字文字标记可以由预处理器连接,就像字符串文字一样:

  const char x [5] =abcd相反,我们得到了撇号(');    ),不被任何写入系统使用我知道作为数字分隔符。



有一个原因,撇号而不是一个简单的空间






,在文本中,保持逗号分裂原本句子的概念,其中句子的功能是终止句子—对我来说,至少,这是一个逗号拆分一个数字的整数部分和一个句点终止准备好分数输入。

解决方案

有一篇论文, n3499 ,它们告诉我们虽然Bjarne自己建议空格作为分隔符:





  • pp-number

    的语法不匹配<

  • 更重要的是,当[af]范围内的十六进制数字遵循一个空格时,会出现一些句法模糊性。

  • 预处理程序不知道是否在空格之后开始执行符号替换。

  • ul>

我想下面的例子是注意到的主要问题:

  const int x = 0x123 a; 

虽然我认为这个理由相当弱。



编辑工具的理由更为糟糕,因为 1' 234 基本上打破人类已知的每个语法高亮笔(例如,上述问题本身中的Markdown使用的高亮笔),并且使更新版本的高亮笔难以实现。



不过,无论是好是坏,这都是导致采用撇号的理由。


As of C++14, thanks to n3781 (which in itself does not answer this question) we may write code like the following:

const int x = 1'234; // one thousand two hundred and thirty four

The aim is to improve on code like this:

const int y = 100000000;

and make it more readable.

The underscore (_) character was already taken in C++11 by user-defined literals, and the comma (,) has localisation problems — many European countries bafflingly use this as the decimal separator — and conflicts with the comma operator, though I do wonder what real-world code could possibly have been broken by allowing e.g. 1,234,567.

Anyway, a better solution would seem to be the space character:

const int z = 1 000 000;

These adjacent numeric literal tokens could be concatenated by the preprocessor just as are string literals:

const char x[5] = "a" "bc" "d";

Instead, we get the apostrophe ('), not used by any writing system I'm aware of as a digit separator.

Is there a reason that the apostrophe was chosen instead of a simple space?


It's baffling because all of those languages, within text, maintain the notion of a comma "breaking apart" an otherwise atomic sentence, with a period functioning to "terminate" the sentence — to me, at least, this is quite analogous to a comma "breaking apart" the integral part of a number and a period "terminating" it ready for the fractional input.

解决方案

There is a previous paper, n3499, which tell us that although Bjarne himself suggested spaces as separators:

While this approach is consistent with one common typeographic style, it suffers from some compatibility problems.

  • It does not match the syntax for a pp-number, and would minimally require extending that syntax.
  • More importantly, there would be some syntactic ambiguity when a hexadecimal digit in the range [a-f] follows a space. The preprocessor would not know whether to perform symbol substitution starting after the space.
  • It would likely make editing tools that grab "words" less reliable.

I guess the following example is the main problem noted:

const int x = 0x123 a;

though in my opinion this rationale is fairly weak. I still can't think of a real-world example to break it.

The "editing tools" rationale is even worse, since 1'234 breaks basically every syntax highlighter known to mankind (e.g. that used by Markdown in the above question itself!) and makes updated versions of said highlighters much harder to implement.

Still, for better or worse, this is the rationale that led to the adoption of apostrophes instead.

这篇关于为什么不选择C ++ 14位数字分隔符的空格字符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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