C++ 应该如何?使用标准

发布于 2024-09-16 10:56:33 字数 542 浏览 4 评论 0原文

我有一个经典的问题,即如何使用 C++ 标准(我的意思是最终确定的实际官方文档)例如 C++98、C++03 来学习和教授 C++。我的想法只是从普通 C++ 用户的角度出发,而不是从语言律师或希望加入标准委员会、编译器编写者等的人的角度出发。

以下是我的个人想法:

a) 这是开始学习 C++ 的好地方。 《C++ in a Nutshell》、《C++ 编程语言》等书籍在这方面做得非常好,同时与标准紧密结合。

时,才需要恢复到标准

  • b) 仅当

    编译器给出的行为与常见书籍所说的不一致,或者

  • 某些行为在编译器之间不一致,例如 GCC、VS、Comeau 等。我知道这些编译器可能不一致的事实是在极少数情况下/语言的黑暗角落,例如模板/异常处理等。但是只有当其中之一正在移植和/或迁移到不同的环境或有编译器升级时,才真正了解可能的不同编译器行为,例如,

  • 如果一个概念在手头的书中解释得不好/没有解释,例如如果它是一个非常先进的概念

对此有什么想法/想法/建议吗?

I have this classic question of how should the C++ Standard (I mean the actual official document of the finalized ones) e.g. C++98, C++03 be used to learn and teach C++. My idea is only from the point of view of an average C++ user and not from the point of view of the language lawyers or someone who wishes to be in the Standards committee, compiler writers and so on.

Here are my personal thoughts:

a) It is aweful place to start learning C++. Books like "C++ in a Nutshell", "The C++ programming Language" etc do a very good job on that front while closely aligning with the Standard.

b) One needs to revert to the Standard only when

  • a compiler gives a behavior which is not consistent with what the common books say or,

  • a certain behavior is inconsistent across compilers e.g. GCC, VS, Comeau etc. I understand the fact that these compilers could be inconsistent is in very few cases / dark corners of the language e.g. templates/exception handling etc. However one really comes to know about the possible different compiler behaviors only when either one is porting and/or migrating to a different environment or when there is a compiler upgrade e.g.

  • if a concept is poorly explained / not explained in the books at hand e.g. if it is a really advanced concept

Any thoughts/ideas/recommendation on this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

流心雨 2024-09-23 10:56:33

从 C++ 语言标准开始学习该语言绝对是一个糟糕的地方。它又密又钝,而且很长。通常,您要查找的信息分布在七个不同的子句中,或者隐藏在与您认为应该在的位置完全无关的子句的半个句子中(或更糟糕的是,您忽略的句子中指定了一种行为,因为您认为它不相关)。

当然,它确实有它的用途。仅举几例,

  • 如果您认为在编译器中发现了错误,通常有必要参考标准以确保您不仅仅是误解了指定的行为是什么。

  • 如果您发现编译器之间的行为不一致,则可以很方便地查找哪个是正确的(或者哪个更正确),尽管通常您需要编写解决方法。

  • 如果您想知道为什么事情是这样的,它通常是一个很好的参考:您可以看到语言的不同功能如何相关并了解它们如何交互。当然,事情并不总是那么清楚,但往往是清楚的。有很多浓缩的例子和注释展示和解释了规范性文本。

  • 如果您在 Stack Overflow 上的帖子中引用 C++ 标准,您会获得更多更多的赞成票。 :-)

  • 学习这门语言非常有趣。编写代码并在编译和运行过程中遇到困难是一回事。尝试从整体上理解该语言并理解为什么必须以某种方式做事,这完全是另一回事。

The C++ language standard would be an absolutely terrible place to start learning the language. It is dense, obtuse, and really long. Often the information you are looking for is spread across seven different clauses or hidden in a half of a sentence in a clause completely unrelated to where you think it should be (or worse, a behavior is specified in the sentence you ignored because you didn't think it was relevant).

It does have its uses, of course. To name a few,

  • If you think you've found a bug in a compiler, it's often necessary to refer to the standard to make sure you aren't just misunderstanding what the specified behavior is.

  • If you find behavior that is inconsistent between compilers, it's handy to be able to look up which is correct (or which is more correct), though often you'll need to write workarounds regardless.

  • If you want to know why things are the way they are, it is often a good reference: you can see how different features of the language are related and understand how they interact. Things aren't always clear, of course, but they often are. There are a lot of condensed examples and notes demonstrating and explaining the normative text.

  • If you reference the C++ standard in a post on Stack Overflow, you get more a lot more upvotes. :-)

  • It's very interesting to learn about the language. It's one thing to write code and stumble through getting things to compile and run. It's another thing altogether to go and try to understand the language as a whole and understand why you have to do things a certain way.

赠佳期 2024-09-23 10:56:33

应使用该标准来确保代码的可移植性。

编写基本的 C++ 代码时,不需要参考标准,但在使用模板或高级使用 STL 时,参考标准对于保持与多个编译器的兼容性以及与未来版本的前向兼容性至关重要。

The standard should be used to ensure portability of code.

When writing basic c++ code you shouldn't need to refer to the standards, but when using templates or advanced use of the STL, reference to the standard is essential to maintain compatibility with more than one compiler, and forward compatibility with future versions.

伴梦长久 2024-09-23 10:56:33

我使用 g++ 编译我的 C++ 程序,并使用选项 -std=c++0x (之前是 -std=c++98) code>) 以确保我的代码始终符合标准。如果我收到任何有关标准合规性的警告或错误,我会对此进行研究以进行自我教育并修复我的代码。

I use g++ to compile my C++ programs and there I use the option -std=c++0x (earlier, -std=c++98) to make sure that my code is always standard compliant. If I get any warning or error regarding standard compliance, I research on that to educate myself and fix my code.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文