Google C + + Style Guide

Source: Internet
Author: User
Tags comments
Google C + + Style Guide

Revision 3.260

Benjy Weinberger
Craig Silverstein
Gregory Eitzmann
Mark Mentovai
Tashana Landray

Each style point has a summary for which additional information are available by toggling the accompanying arrow button tha T looks this way:▽ to toggle all summaries with the big arrow button:▽toggle all summaries Table of Contents

Header Files The #define Guard Forward Declarations Inline functions the-inl.h Files Function Parameter ordering Names and order of Cludes
Scoping Namespaces Nested Classes nonmember, static member, and global functions the local Variables Static and global Variables
Classes Doing Work in constructors Default constructors Explicit Constructors Copy Constructors Structs vs. Classes inheritance Mu Ltiple Inheritance Interfaces Operator overloading Access Control declaration order Write Short functions
Google-specific Magic Smart Pointers Cpplint
Other C + + Features Reference Arguments Function overloading Default Arguments variable-length Arrays and Alloca () Friends exceptions Type information (RTTI) casting Streams preincrement and predecrement use of const Integer Types 64-bit Portability Prepr Ocessor Macros 0 and nullptr/null sizeof auto brace initialization Boost c++11 unique_ptr
Naming General naming Rules File Names Type Names Variable Names Constant Names Function Names Namespace Names Enumerator Names M Acro Names exceptions to naming Rules
Comments Comment Style File Comments Class Comments Function Comments Variable Comments Implementation Comments punctuation, Spelli Ng and grammar TODO Comments deprecation Comments
Formatting Line Length non-ascii Characters spaces vs. Tabs function Declarations and definitions function Calls braced initializer L ists conditionals Loops and Switch statements pointer and Reference Expressions Boolean Expressions return Values Variable and Array initialization preprocessor directives Class Format constructor initializer Lists Namespace formatting Al whitespace Vertical whitespace
Exceptions to the Rules Existing non-conformant Code Windows Code
Important Note displaying Hidden Details in this GuideLink▽this style Guide contains many details that are initially the from view. They are marked by the triangle icon, which you are here on your left. Click it now. You should "Hooray" appear below.

hooray! Now, you know can expand points for more details. Alternatively, there's "expand all" in the top's this document. Background

C + + is the main development language used by many of Google ' s open-source projects. As every C + + programmer knows, the language has many powerful-features, but this power brings with it complexity, which in Turn can make code more bug-prone and harder to read and maintain.

The goal of this guide be to manage this complexity by describing in detail the DOS and don ' ts of writing C + + code. These rules are exist to keep the code base manageable while still allowing coders to use C + + language features.

Style, also known as readability, is what "we call" conventions that govern our C + + code. The term Style is a bit of a misnomer since this conventions cover far more than source file just.

One way in which we keep the code base manageable are by enforcing consistency. It is very important so any programmer being able to look at another ' s code and quickly understand it. Maintaining a uniform style and following conventions means ' we can more easily use ' pattern-matching ' to infer what V Arious symbols are and what invariants are true about them. Creating common, required idioms and patterns code much makes to easier. In some cases there might is good arguments for changing certain style rules, but we nonetheless keep things as they I n order to preserve consistency.

Another issue This guide addresses be that of C + + feature bloat. C + + is a huge language with many advanced features. In some cases we constrain, or even ban, use of certain features. We avoid the various common errors and problems can features the "We do" to keep code. This guide lists these features and explains why their the use is restricted.

Open-source projects developed by Google conform to the requirements in this guide.

The note, this, is not a C + + Tutorial:we Assume that the reader are familiar with the language. Header Files

In general, every. cc file should have a associated. h file. There are some common exceptions, such as unittests and Small. cc files containing just a main () function.

Correct use of header files can make a huge difference to the readability, size and performance of your code.

The following rules would guide you through the various pitfalls of using header files. The #define Guard link▽all header files should have #define guards to prevent multiple. The format of the symbol name should is <project>_<path>_<file>_h_.

To guarantee uniqueness, they should is based on the "full path" in a project's source tree. For example, the file foo/src/bar/baz.h in Project Foo should have the following guard:

#ifndef foo_bar_baz_h_
#define Foo_bar_baz_h_ ...

#endif  //Foo_bar_baz_h_
Forward DeclarationsLink▽you may forward declare ordinary classes into order to avoid unnecessary #includes.

Definition:a "Forward declaration" is a declaration of a class, function, or template without A associated definition. The #include lines can often to replaced with forward declarations of whatever symbols, are actually used by the client code.

Pros:unnecessary #includes Force the compiler to open more files and process more input. They can also force your code to is recompiled more often, due to changes in the header.

Cons:it can be difficult to determine the correct form of a forward declaration in the presence of features like template S, typedefs, default parameters, and using declarations. It can be difficult to determine whether a forward declaration/a full #include was needed for a given piece of code, par Ticularly When implicit conversion operations are involved. In extreme cases, replacing a #include with a forward declaration can silently change the meaning of code. Forward declaring multiple symbols from a header can is more verbose than simply #includeing the header. Forward declarations of functions and templates can prevent the header owners from making otherwise-compatible to their APIs; For example, widening a parameter type, or adding a template parameter with a default value. Forward declaring symbols from namespace std:: Usually, yields undefined. Structuring code to enable forward declarations (e.g. using pointer members instead an object members) can make the COde slower and more complex. The practical efficiency benefits of forward declarations are.

Decision:when using a function declared in a header file, the always #include that header. When using a class template, the prefer to #include its header file. When using the ordinary class, relying on a forward declaration are OK, but be wary of situations where a forward n may be insufficient or incorrect; When in doubt, just #include the appropriate header. Do not replace the pointers just to avoid a #include. Always #include the file that actually provides the declarations/definitions for you need; Do isn't rely on the symbol being brought in transitively via headers not directly. One exception is to that myfile.cc rely in #includes and forward declarations from its corresponding header file myfile.h .

Inline functions link▽define functions Inline They are, small, or say.

Definition:you can declare functions in a way which allows the compiler to expand them inline rather than calling them the THR Ough the usual function call mechanism.

Pros:inlining a function can generate more efficient object code, as long as the inlined function is small. Feel free to inline accessors and mutators, and other short, performance-critical functions.

Cons:overuse of inlining can actually make programs slower. Depending on a function ' s size, inlining it can cause the code size to increase or decrease. inlining a very small accessor function would usually decrease code size while inlining a very large function can Dramatica lly increase code size. On modern processors smaller code usually runs faster due to better use of the instruction cache.

Decision:

A decent rule of thumb was to not inline a function if it was more than lines long. Beware of destructors, which are often longer than they appear because of implicit member-and base-destructor calls!

Another useful rule of thumb:it ' s typically not cost effective to inline functions with loops or switch statements (unles s, in the common case, the loop or switch statement is never executed).

It is important to know this functions are not always inlined even if they are as declared; For example, the virtual and recursive functions are not normally inlined. Usually recursive functions should not be inline. The main reason for making a virtual function inline are to place their definition in the class, either for convenience or to Document its behavior, e.g., for accessors and mutators.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.