C++ 官方风格指南的链接

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2434213/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 23:28:13  来源:igfitidea点击:

Links to official style guides

c++coding-style

提问by User1

C++ has several types of styles: MFC, Boost, Google, etc. I would like to examine these styles and determine which one is best for my projects, but I want to read from the official style guidebook. Does anyone have an official guide that they typically use?

C++ 有几种类型的样式:MFC、Boost、Google 等。我想检查这些样式并确定哪一种最适合我的项目,但我想从官方样式指南中阅读。有没有人有他们通常使用的官方指南?

Here are two that I found. I bet there are more:

这是我找到的两个。我敢打赌还有更多:

Note: This is NOT a discussion about which style is best... only a call for official style guides that people currently use. Please refrain from bashing other style guides that you don't like.

注意:这不是关于哪种风格最好的讨论……只是呼吁人们目前使用的官方风格指南。请不要抨击您不喜欢的其他风格指南。

Side question: Is there a good tool that can examine source code and tell if it matches a given style guide?

附带问题:是否有一个好的工具可以检查源代码并判断它是否与给定的样式指南匹配?

采纳答案by dirkgently

Not a Coding Guideline per se, but I find this mighty useful: Bjarne Stroustrup's C++ Style and Technique FAQ

本身不是编码指南,但我发现这非常有用:Bjarne Stroustrup 的 C++ 风格和技术常见问题解答

回答by dirkgently

There's no such thing as an "official" style guide - the C++ standard is entirely silent on style. One book on the subject by two highly knowledgable C++ guys is C++ Coding Standardsby Sutter & Alexandrescu.

没有“官方”风格指南这样的东西——C++ 标准对风格完全保持沉默。Sutter 和 Alexandrescu 所著的C++ 编码标准是由两位知识渊博的 C++ 人员撰写的一本关于该主题的书。

回答by gnavi

I have also written some tips for good coding in c++: http://www.ivanism.com/Articles/CodingStandards.html

我还写了一些在 C++ 中进行良好编码的技巧:http: //www.ivanism.com/Articles/CodingStandards.html

The post starts with:

帖子开头是:

The goal of coding standards are to increase the business value of the code. The most obvious (and indeed most important) way to do this is to make the code robust and low defect. Equally important, but more subtle goals include reducing coder friction and maintainability. As such, standards should be kept minimal -- simple enough to actually follow, and important enough to remember.

编码标准的目标是增加代码的商业价值。最明显(也是最重要)的方法是使代码健壮和低缺陷。同样重要但更微妙的目标包括减少编码器摩擦和可维护性。因此,标准应该保持最低限度——简单到可以实际遵循,并且重要到可以记住。

These standards should be used when building new source files. When an existing file needs to be changed, that is an appropriate time to bring it up to standard. However, it's never a good time to edit a file merely to bring it up to standard. If it ain't broke, don't "fix it" and remember to always "Keep it Working".

构建新源文件时应使用这些标准。当需要更改现有文件时,这是使其达到标准的合适时机。然而,仅仅为了使其达到标准而编辑文件从来都不是一个好时机。如果它没有坏,请不要“修复它”并记住始终“保持工作”。

You'll notice that I don't touch on the classic "Religious" points:

你会注意到我没有触及经典的“宗教”观点:

 tabs vs. spaces
 indentation style
 curly brace style
 etc...

Consistency within a file is important and improves readability. But allowing coders to express themselves is also important. So, if you edit a file, either conform to the religion of that file, or convert the whole file to a new, consistent format. If you convert the whole file, you are effectively taking ownership of it, so be prepared to be the go-to person, or leave it as is.

文件内的一致性很重要,可以提高可读性。但允许编码人员表达自己也很重要。因此,如果您编辑一个文件,要么符合该文件的宗教信仰,要么将整个文件转换为新的、一致的格式。如果您转换整个文件,您实际上是在获得它的所有权,所以准备好成为首选人,或者保持原样。

回答by Meyer

Other answers state that there is no official style guide, which was true at the time.

其他答案指出没有官方风格指南,这在当时是正确的。

But in 2015, Bjarne Stroustrup announced the C++ Core Guidelines, an open source project to build authoritative guidelines for modern C++ code, led by Stroustrup himself and published by the Standard C++ Foundation:

但在 2015 年,Bjarne Stroustrup宣布了 C++ Core Guidelines,这是一个为现代 C++ 代码构建权威指南的开源项目,由 Stroustrup 本人领导并由标准 C++ 基金会发布:

http://github.com/isocpp/CppCoreGuidelines

http://github.com/isocpp/CppCoreGuidelines

In relevance to this question, the Core Guidelines also link to other guidelines, with added comments. About the often recommended Google C++ Style Guide, they say:

与此问题相关,核心指南还链接到其他指南,并添加了评论。关于经常推荐的 Google C++ Style Guide,他们说:

Geared toward C++03 and (also) older code bases. Google experts are now actively collaborating here on helping to improve these Guidelines, and hopefully to merge efforts so these can be a modern common set they could also recommend.

面向 C++03 和(也)旧代码库。谷歌专家现在正在积极合作帮助改进这些指南,并希望合并努力,使这些可以成为他们也可以推荐的现代通用集。

回答by Ozan

Another style guide are the The JSF air vehicle C++ coding standards.

另一个风格指南是JSF 飞行器 C++ 编码标准

回答by Mike Seymour

C++ doesn't have and doesn't need an official style. Many organisations impose style guides on their contributors to try to maintain some kind of corporate look and feel; some of these contain snippets of good advice, but many just force you to add strange decorations that seemed like a good idea to someone writing a completely different language in the 1980s.

C++ 没有也不需要官方风格。许多组织将风格指南强加给他们的贡献者,以试图保持某种公司的外观和感觉;其中一些包含一些好的建议,但许多只是强迫您添加奇怪的装饰,这对于 1980 年代编写完全不同的语言的人来说似乎是个好主意。

The only really useful advice you'll find amongst the waffle is:

你会在华夫饼中找到的唯一真正有用的建议是:

  • Define a consistent way to distinguish types, objects, and some kinds of function (such as accessors and factories), so you'll know to write (for example) Thing thing = GetThing();without looking the names up.
  • Don't start names with underscores. This is forbidden in some circumstances, and it's simpler and more readable to not do it at all than to worry about exactly when you can.
  • Spare a thought for the poor chap (perhaps you) who has to read and maintain the code in a few years' time.
  • Keep it simple.
  • Use your brain.
  • 定义一种一致的方法来区分类型、对象和某些类型的函数(例如访问器和工厂),这样您就知道如何编写(例如)Thing thing = GetThing();而无需查找名称。
  • 不要以下划线开头的名称。这在某些情况下是被禁止的,完全不这样做比担心什么时候可以做更简单、更易读。
  • 想想几年后必须阅读和维护代码的可怜人(也许是你)。
  • 把事情简单化。
  • 用你的大脑。

回答by philsquared

I use my own style, which I have written up here. Whether or not you are interested in it as a style, if you're looking at styles in general you may find my discussion on the motivation for it to be useful.

我使用我自己的风格,我写在这里。无论您是否对它作为一种样式感兴趣,如果您正在查看一般样式,您可能会发现我关于它的动机的讨论很有用。

回答by greyfade

To the side question: I don't personally know of any tools that analyze the style in use, but there are tools that reformat source to a given style guide. One that comes to mind is Artistic Style.

附带问题:我个人不知道有任何工具可以分析正在使用的样式,但是有一些工具可以将源重新格式化为给定的样式指南。一个想到的是艺术风格

回答by Thomas Matthews

As for the side question, what you need is a static analysis tool. An expensive and huge tool is Klocwork. I've used it at a couple of shops and it can be set up to issue warnings against style issues. I don't recommend it for single users; it is more for a corporate environment. Although they may have stripped down versions for individuals.

至于附带问题,您需要的是静态分析工具Klocwork是一个昂贵而庞大的工具。我在几家商店使用过它,它可以设置为针对风格问题发出警告。我不推荐给单个用户;它更适用于企业环境。尽管他们可能已经精简了个人版本。

Remember to Google for static analysis tools.

请记住谷歌的静态分析工具。