C++ style guide: Difference between revisions

320 bytes removed ,  24 August 2021
Line 7: Line 7:


== Formatting ==
== Formatting ==
=== Line Length ===
Maximal length of source lines is 79 characters.


=== Indentation ===
=== Indentation ===


Use only spaces, and indent 2 spaces at a time.
* Use only spaces.
* Tabs are prohibited.
* Indent with 2 spaces at a time.


We use spaces for indentation. Absolutely do not use tabs in your code.
==== Control structures (if, while, ...) ====
You should probably set your editor to emit spaces when you hit the tab key.


When indenting, indent the statement after control
When indenting, indent the statement after control
Line 19: Line 24:
is a compound statement, indent ''both'' the curly braces and the
is a compound statement, indent ''both'' the curly braces and the
body of the statement (so that the body gets indented by ''two''
body of the statement (so that the body gets indented by ''two''
indents). This format is known as "GNU style" and is an option for
indents).
some code formatting tools.  Example indenting:
 
Example:


<syntaxhighlight lang="cpp">
<syntaxhighlight lang="cpp">
Line 34: Line 40:
If you have nested {{codeline|if}} statements, use extra braces for extra
If you have nested {{codeline|if}} statements, use extra braces for extra
clarification.
clarification.
==== Split long expressions ====


Split long expressions in such a way that a continuation line starts
Split long expressions in such a way that a continuation line starts
Line 66: Line 74:
unsigned int& reference_variable;
unsigned int& reference_variable;
</syntaxhighlight>
</syntaxhighlight>
=== Line Length ===
Keep the length of source lines to 79 characters or less, for maximum
readability in the widest range of environments.  This is inherited from
the [https://www.gnu.org/prep/standards/standards.html#Formatting GNU Coding Standards].


=== Function headers ===
=== Function headers ===