Google ' s C + + coding style__c++

Source: Internet
Author: User
Tags function definition

V0.2-last updated November 8, 2013

Originated from Google's C + + coding style Rev 3.274 catalog generated by Doctoc
Header file
#define用法
Forward declaration
inline function
-inl.h file
function parameter Order
Name and order of include
Scope
Name space
Unnamed space
Name space
Nested classes
Non-member functions, static member functions, global functions
Local variables
Static variables and global variables
Class
Complete the work inside the constructor
Class
An explicit constructor
Copy Constructors
Delegating and inheriting constructors
Structure vs Class
Inherited
Multiple inheritance
Interface
Operator overloading
Access control
Declaration Order
Writing short functions
Other C + + features
Ownership and smart pointers
Reference parameters
Right value reference
Function overload
Default parameters
Variable-length arrays and alloca
Friend Yuan
Abnormal
Run-Time type recognition
Transformation
Flow
Pre-self-increase and self-reduction
Const usage
CONSTEXPR usage
Integer type
Unsigned integers type
64-bit portability
Preprocessing macros
0 and Nullptr/null
sizeof
Auto
Curly braces Initialization
Lambda expression
Boost
C++11
General naming rules
Filename
Type name
Variable name
Generic variable Name
Class data members
Structural Body variables
Global variables
Constant name
Function name
General functions
Accessors and storage
Name of the namespace
Enumerator name
Macro naming
Naming rules for exceptions
Bigopen ()
UInt
Bigpos
Sparse_hash_map
Longlong_max
Comments
Doxygen
Annotation specification
File comments
Legal Notices and authors
File contents
class annotation
function comment
function declaration
function definition
Variable annotation
Class Members
Global variables
Implementing annotations
Class data members
Single-line Comment
Nullptr/null, True/false, 1, 2, 3 ...
Don ' TS
Punctuation, spelling and grammar
TODO comments
Discard comments
Formatting
Line length
Non-ASCII characters
A space or a tab-stop
function declaration and definition
Function call
Curly Braces Initialization List
Conditional statement
Loops and Selection statements
Pointers and reference expressions
Boolean expression
return value
Variable and array initialization
Preprocessor directives
Class format
Constructor initialization list
namespace formatting
Horizontal whitespace
So so
Loops and Conditions
Operator
Stencil and type conversions
Vertical whitespace
Rules of the exception
Existing code that does not conform to the standard
Windows code
header File

In general, each. cpp file should have a related H file. There are some common exceptions, such as unit test code and CPP files that contain only one main function.

The correct use of header files varies greatly in readability, file size, and performance.

The following rules will guide you through the various traps in the use of the header file. Define Usage

All header files should be protected by #define to avoid multiple inclusions. The format of the symbol name should be <project>_<path>_<file>_h_.

To ensure uniqueness, they should be based on the full path of the project's source code tree. For example, the Foo project cocos2dx/sprites_nodes/ccsprite.h in a file should have the following defenses:

1 2 3 4 5 6 #ifndef cocos2dx_sprite_nodes_ccsprite_h_ #define COCOS2DX_SPRITE_NODES_CCSPRITE_H_ ... #endif//Cocos2dx_sprite_nod Es_ccsprite_h_
1 2 Pragma once is still open for debate #pragma once

We are considering whether to use #pragma once, and we are not sure that he can support all platforms. Forward Declaration

The forward declaration generic class can avoid unnecessary #includes.

definition:"forward declaration" is a declaration of a class, function, or template, and is not defined. Replacing #include with forward declarations is typically applied to client code.

Advantages: Unnecessary #includes forces the compiler to open more files and process more input. Unnecessary #includes can also cause code to be recompiled more often because the header file is modified.

Disadvantages: It is not easy to determine the template, typedefs, default parameters, such as forward declarations and the use of declarations. It is not easy to tell whether to use a forward declaration or a #include for a given code, especially if there is an implicit conversion. In extreme cases, using #include instead of the forward declaration will silently change the meaning of the code. Multiple forward declarations in a header file are more verbose than #include. A forward declaration function or template prevents the header file from being "otherwise compatible" (otherwise-compatible) modified by the APIs, such as extending the parameter type or adding a template parameter with a default value. A forward declaration of a symbol for an STD namespace usually results in an indeterminate behavior. Structured code for forward declarations (for example, the application of pointer members rather than object members) makes the code slower and more complex. The actual efficiency improvement of the forward declaration is unconfirmed.

Conclusion: use the function declared in the header file to always #include the header file. Using the class template, priority is given to using #include. Use of ordinary classes, you can use the forward declaration, but note that the forward declaration may not be enough or incorrect situation, if you have questions, use #include. You should not use pointer members instead of data members just to avoid #include.

Always #include the actual declaration/definition of the file; Do not rely on symbols that are introduced indirectly in a header file that is not directly included. The exception is that Myfile.cpp can rely on #include and forward declarations in Myfile.h. Inline function

It is defined as an inline function only when the function body is small--10 line code.

definition: You can declare a function by allowing the compiler to extend it inline, instead of calling it through a common function call mechanism.

Benefits:   Inline, dapper functions can generate more efficient object code. Recommended inline value functions, set value functions, and the rest of the performance-critical short functions.

Related Article

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.