C ++ 1z为什么不删除二字图连同三字母? [英] C++1z why not remove digraphs along with trigraphs?

查看:218
本文介绍了C ++ 1z为什么不删除二字图连同三字母?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

C ++ 1z将删除三字母。 IBM严重反对这一点(此处在这里),所以似乎有双方的移除/不移除的论据。



但是,由于决定删除三字母,为什么要离开连字符?我没有看到任何理由保持二字符超出了保持三字母的原因(它显然没有足够的重量来保持它们)。

解决方案

Trigraphs对有意识的用户比有向图更有问题。这是因为它们在字符串文字和注释中被替换。以下是一些示例...



示例A:

  std :: string example =What ??!??! 
std :: cout<<示例< std :: endl;

什么|| 安慰。这是因为三叉 ??!被翻译为 |



示例B:

  //错误?! !?? / 
std :: cout<< 有一个错误! << std :: end;

不会发生任何事情。这是因为 ?? / 转换为 \ ,它转义换行符并导致下一行被注释

> //这没有意义?!?!?!??!/ / b $ b std :: string example =Hello World;
std :: cout<<示例< std :: endl;

这将给出一个错误,沿着使用未声明标识符,原因与示例B相同。



三字母也可能导致更复杂的问题,值得注意的是,许多编译器实际上在进行翻译时发出警告;另一个原因,总是把警告作为错误。



字母比三字母的问题少得多,因为它们不是在另一个标记中替换的(即字符串或字符文字),并且没有转换为 \ 的序列,因此不能在注释中转义新行。



结论



除了更难于阅读代码之外,二进制图导致的问题也更少,因此需要删除它们。


C++1z will remove trigraphs. IBM heavily opposed this (here and here) so there seem to be arguments for both sides of removal/non removal.

But since the decision was made to remove trigraphs, why leave digraphs? I don't see any reasons for keeping digraphs beyond the reasons to keep trigraphs (which apparently didn't weight enough to keep them).

解决方案

Trigraphs are more problematic to the unaware user than digraphs. This is because they are replaced within string literals and comments. Here are some examples…

Example A:

std::string example = "What??!??!";
std::cout << example << std::endl;

What|| will be printed to the console. This is because of the trigraph ??! being translated to |.

Example B:

// Error ?!?!?!??!??/
std::cout << "There was an error!" << std::end;

Nothing will happen at all. This is because ??/ translates to \, which escapes the newline character and results in the next line being commented out.

Example C:

// This makes no sense ?!?!!?!??!??/
std::string example = "Hello World";
std::cout << example << std::endl;

This will give an error along the lines of use of undeclared identifier "example" for the same reasons as Example B.

There are far more elaborate problems trigraphs can cause too, but you get the idea. It's worth noting that many compilers actually emit a warning when such translations are being made; yet another reason to always treat warnings as errors. However this is not required by the standard and therefore cannot be relied upon.

Digraphs are much less problematic than trigraphs, as they are not replaced inside another token (i.e. a string or character literal) and there is not a sequence that translates to \, so escaping new lines in comments cannot occur.

Conclusion

Other than harder to read code, there are less problems caused by digraphs and therefore the need to remove them is greatly reduced.

这篇关于C ++ 1z为什么不删除二字图连同三字母?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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