Coding Style
Coding Style
Coding Style
DOCTYPE html>
<html lang="en" class=" is-copy-enabled">
<head prefix="og: http://ogp.me/ns# fb: http://ogp.me/ns/fb# object: http://og
p.me/ns/object# article: http://ogp.me/ns/article# profile: http://ogp.me/ns/pro
file#">
<meta charset='utf-8'>
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta http-equiv="Content-Language" content="en">
<meta name="viewport" content="width=1020">
<title>cocos2d-x/CODING_STYLE.md at v3 cocos2d/cocos2d-x GitHub</title>
<link rel="search" type="application/opensearchdescription+xml" href="/opens
earch.xml" title="GitHub">
<link rel="fluid-icon" href="https://github.com/fluidicon.png" title="GitHub
">
<link rel="apple-touch-icon" sizes="57x57" href="/apple-touch-icon-114.png">
<link rel="apple-touch-icon" sizes="114x114" href="/apple-touch-icon-114.png
">
<link rel="apple-touch-icon" sizes="72x72" href="/apple-touch-icon-144.png">
<link rel="apple-touch-icon" sizes="144x144" href="/apple-touch-icon-144.png
">
<meta property="fb:app_id" content="1401488693436528">
<meta content="@github" name="twitter:site" /><meta content="summary" name
="twitter:card" /><meta content="cocos2d/cocos2d-x" name="twitter:title" /><meta
content="cocos2d-x for C++" name="twitter:description" /><meta content="https:/
/avatars0.githubusercontent.com/u/234324?v=3&s=400" name="twitter:image:src"
/>
<meta content="GitHub" property="og:site_name" /><meta content="object" pr
operty="og:type" /><meta content="https://avatars0.githubusercontent.com/u/23432
4?v=3&s=400" property="og:image" /><meta content="cocos2d/cocos2d-x" propert
y="og:title" /><meta content="https://github.com/cocos2d/cocos2d-x" property="og
:url" /><meta content="cocos2d-x for C++" property="og:description" />
<meta name="browser-stats-url" content="https://api.github.com/_private/br
owser/stats">
<meta name="browser-errors-url" content="https://api.github.com/_private/bro
wser/errors">
<link rel="assets" href="https://assets-cdn.github.com/">
<meta name="pjax-timeout" content="1000">
<meta name="msapplication-TileImage" content="/windows-tile.png">
<meta name="msapplication-TileColor" content="#ffffff">
<meta name="selected-link" value="repo_source" data-pjax-transient>
<meta name="google-site-verification" content="KT5gs8h0wvaagLKAVWq8bbeNwnZZK
1r1XQysX3xurLU">
<meta name="google-analytics" content="UA-3769691-2">
<meta content="collector.githubapp.com" name="octolytics-host" /><meta conte
nt="collector-cdn.github.com" name="octolytics-script-host" /><meta content="git
hub" name="octolytics-app-id" /><meta content="7371CB06:711A:664E5D1:55E3E19A" n
ame="octolytics-dimension-request_id" />
<meta content="Rails, view, blob#show" data-pjax-transient="true" name="anal
ytics-event" />
<meta class="js-ga-set" name="dimension1" content="Logged Out">
<meta class="js-ga-set" name="dimension4" content="Current repo nav">
<meta name="is-dotcom" content="true">
<meta name="hostname" content="github.com">
<meta name="user-login" content="">
<link rel="icon" sizes="any" mask href="https://assets-cdn.github.com/pinn
ed-octocat.svg">
<meta name="theme-color" content="#4078c0">
<link rel="icon" type="image/x-icon" href="https://assets-cdn.github.com/f
avicon.ico">
<!-- </textarea> --><!-- '"` --><meta content="authenticity_token" name="csr
f-param" />
<meta content="nzfrsh9ACG7pIpwGXtIvcdOQaqj9uNYJ+603DKCNmhEkdwiTonohXw15HnVr8aTjV
hDC4oegbmBo/4/cD7i2tw==" name="csrf-token" />
<link crossorigin="anonymous" href="https://assets-cdn.github.com/assets/git
hub/index-ebd09ebc92d1048dd7af5cd68484181a6a6a0260b3df1e7349053db235e6c53a.css"
media="all" rel="stylesheet" />
<link crossorigin="anonymous" href="https://assets-cdn.github.com/assets/git
hub2/index-f20fa2d1dc1467ff8e2f823d0c693c4aba5668d5b740a4c567afc32f85422020.css"
media="all" rel="stylesheet" />
</div>
</div>
<span class="js-pull-replace-counter"></span>
<img alt="" class="mini-loader" height="16" src="https://assets-cdn.gi
thub.com/images/spinners/octocat-spinner-32.gif" width="16" />
</a>
</li>
<li class="tooltipped tooltipped-w" aria-label="Wiki">
<a href="/cocos2d/cocos2d-x/wiki" aria-label="Wiki" class="js-selected-n
avigation-item sunken-menu-item" data-hotkey="g w" data-selected-links="repo_wik
i /cocos2d/cocos2d-x/wiki">
<span class="octicon octicon-book"></span> <span class="full-word">Wik
i</span>
<img alt="" class="mini-loader" height="16" src="https://assets-cdn.gi
thub.com/images/spinners/octocat-spinner-32.gif" width="16" />
</a>
</li>
</ul>
<div class="sunken-menu-separator"></div>
<ul class="sunken-menu-group">
<li class="tooltipped tooltipped-w" aria-label="Pulse">
<a href="/cocos2d/cocos2d-x/pulse" aria-label="Pulse" class="js-selected-n
avigation-item sunken-menu-item" data-selected-links="pulse /cocos2d/cocos2d-x/p
ulse">
<span class="octicon octicon-pulse"></span> <span class="full-word">Puls
e</span>
<img alt="" class="mini-loader" height="16" src="https://assets-cdn.gith
ub.com/images/spinners/octocat-spinner-32.gif" width="16" />
</a>
</li>
<li class="tooltipped tooltipped-w" aria-label="Graphs">
<a href="/cocos2d/cocos2d-x/graphs" aria-label="Graphs" class="js-selected
-navigation-item sunken-menu-item" data-selected-links="repo_graphs repo_contrib
utors /cocos2d/cocos2d-x/graphs">
<span class="octicon octicon-graph"></span> <span class="full-word">Grap
hs</span>
<img alt="" class="mini-loader" height="16" src="https://assets-cdn.gith
ub.com/images/spinners/octocat-spinner-32.gif" width="16" />
</a>
</li>
</ul>
</nav>
<div class="only-with-full-nav">
<div class="js-clone-url clone-url open"
data-protocol-type="http">
<h3><span class="text-emphasized">HTTPS</span> clone URL</h3>
<div class="input-group js-zeroclipboard-container">
<input type="text" class="input-mini input-monospace js-url-field js-zerocli
pboard-target"
value="https://github.com/cocos2d/cocos2d-x.git" readonly="readonly"
aria-label="HTTPS clone URL">
<span class="input-group-button">
<button aria-label="Copy to clipboard" class="js-zeroclipboard btn btn-sm
zeroclipboard-button tooltipped tooltipped-s" data-copied-hint="Copied!" type="b
utton"><span class="octicon octicon-clippy"></span></button>
</span>
</div>
</div>
</div>
<div id="js-repo-pjax-container" class="repository-content context-loade
r-container" data-pjax-container>
<a href="/cocos2d/cocos2d-x/blob/20d7abe6f308009babb27ca001d80695fe997a65/docs/C
ODING_STYLE.md" class="hidden js-permalink-shortcut" data-hotkey="y">Permalink</
a>
<!-- blob contrib key: blob_contributors:v21:ea729937e09308e92a917130c20c50fa ->
<div class="file-navigation js-zeroclipboard-container">
<div class="select-menu js-menu-container js-select-menu left">
<span class="btn btn-sm select-menu-button js-menu-target css-truncate" data-h
otkey="w"
data-ref="v3"
title="v3"
role="button" aria-label="Switch branches or tags" tabindex="0" aria-haspopu
p="true">
<i>Branch:</i>
<span class="js-select-button css-truncate-target">v3</span>
</span>
<div class="select-menu-modal-holder js-menu-content js-navigation-container"
data-pjax aria-hidden="true">
<div class="select-menu-modal">
<div class="select-menu-header">
<span class="select-menu-title">Switch branches/tags</span>
<span class="octicon octicon-x js-menu-close" role="button" aria-label="
Close"></span>
</div>
<div class="select-menu-filters">
<div class="select-menu-text-filter">
<input type="text" aria-label="Filter branches/tags" id="context-commi
tish-filter-field" class="js-filterable-field js-navigation-enable" placeholder=
"Filter branches/tags">
</div>
<div class="select-menu-tabs">
<ul>
<li class="select-menu-tab">
<a href="#" data-tab-filter="branches" data-filter-placeholder="Fi
lter branches/tags" class="js-select-menu-tab" role="tab">Branches</a>
</li>
<li class="select-menu-tab">
<a href="#" data-tab-filter="tags" data-filter-placeholder="Find a
tag " class="js-select-menu-tab" role="tab">Tags</a>
</li>
</ul>
</div>
</div>
<div class="select-menu-list select-menu-tab-bucket js-select-menu-tab-buc
ket" data-tab-filter="branches" role="menu">
<div data-filterable-for="context-commitish-filter-field" data-filterabl
e-type="substring">
<a class="select-menu-item js-navigation-item js-navigation-open "
href="/cocos2d/cocos2d-x/blob/v1/docs/CODING_STYLE.md"
data-name="v1"
data-skip-pjax="true"
rel="nofollow">
<span class="select-menu-item-icon octicon octicon-check"></span>
<span class="select-menu-item-text css-truncate-target" title="v1"
>
v1
</span>
</a>
<a class="select-menu-item js-navigation-item js-navigation-open "
href="/cocos2d/cocos2d-x/blob/v2/docs/CODING_STYLE.md"
data-name="v2"
data-skip-pjax="true"
rel="nofollow">
<span class="select-menu-item-icon octicon octicon-check"></span>
<span class="select-menu-item-text css-truncate-target" title="v2"
>
v2
</span>
</a>
<a class="select-menu-item js-navigation-item js-navigation-open sel
ected"
href="/cocos2d/cocos2d-x/blob/v3/docs/CODING_STYLE.md"
data-name="v3"
data-skip-pjax="true"
rel="nofollow">
<span class="select-menu-item-icon octicon octicon-check"></span>
<span class="select-menu-item-text css-truncate-target" title="v3"
>
v3
</span>
</a>
<a class="select-menu-item js-navigation-item js-navigation-open "
href="/cocos2d/cocos2d-x/blob/v3.8/docs/CODING_STYLE.md"
data-name="v3.8"
data-skip-pjax="true"
rel="nofollow">
<span class="select-menu-item-icon octicon octicon-check"></span>
<span class="select-menu-item-text css-truncate-target" title="v3.
8">
v3.8
</span>
</a>
<a class="select-menu-item js-navigation-item js-navigation-open "
href="/cocos2d/cocos2d-x/blob/v3-doc/docs/CODING_STYLE.md"
data-name="v3-doc"
data-skip-pjax="true"
rel="nofollow">
<span class="select-menu-item-icon octicon octicon-check"></span>
<span class="select-menu-item-text css-truncate-target" title="v3doc">
v3-doc
</span>
</a>
<a class="select-menu-item js-navigation-item js-navigation-open "
href="/cocos2d/cocos2d-x/blob/v4-develop/docs/CODING_STYLE.md"
data-name="v4-develop"
data-skip-pjax="true"
rel="nofollow">
<span class="select-menu-item-icon octicon octicon-check"></span>
<span class="select-menu-item-text css-truncate-target" title="v4develop">
v4-develop
</span>
</a>
<a class="select-menu-item js-navigation-item js-navigation-open "
href="/cocos2d/cocos2d-x/blob/v35-for-tizen/docs/CODING_STYLE.md"
data-name="v35-for-tizen"
data-skip-pjax="true"
rel="nofollow">
<span class="select-menu-item-icon octicon octicon-check"></span>
<span class="select-menu-item-text css-truncate-target" title="v35
-for-tizen">
v35-for-tizen
</span>
</a>
</div>
<div class="select-menu-no-results">Nothing to show</div>
</div>
<div class="select-menu-list select-menu-tab-bucket js-select-menu-tab-buc
ket" data-tab-filter="tags">
<div data-filterable-for="context-commitish-filter-field" data-filterabl
e-type="substring">
<div class="select-menu-item js-navigation-item ">
<span class="select-menu-item-icon octicon octicon-check"></span>
<a href="/cocos2d/cocos2d-x/tree/cocos2d-x-4.0alpha1/docs/CODING_S
TYLE.md"
data-name="cocos2d-x-4.0alpha1"
data-skip-pjax="true"
rel="nofollow"
class="js-navigation-open select-menu-item-text css-truncate-ta
rget"
title="cocos2d-x-4.0alpha1">cocos2d-x-4.0alpha1</a>
</div>
<div class="select-menu-item js-navigation-item ">
<span class="select-menu-item-icon octicon octicon-check"></span>
<a href="/cocos2d/cocos2d-x/tree/cocos2d-x-4.0alpha0/docs/CODING_S
TYLE.md"
data-name="cocos2d-x-4.0alpha0"
data-skip-pjax="true"
rel="nofollow"
class="js-navigation-open select-menu-item-text css-truncate-ta
rget"
title="cocos2d-x-4.0alpha0">cocos2d-x-4.0alpha0</a>
</div>
<div class="select-menu-item js-navigation-item ">
<span class="select-menu-item-icon octicon octicon-check"></span>
<a href="/cocos2d/cocos2d-x/tree/cocos2d-x-3.8rc0/docs/CODING_STYL
E.md"
data-name="cocos2d-x-3.8rc0"
data-skip-pjax="true"
rel="nofollow"
class="js-navigation-open select-menu-item-text css-truncate-ta
rget"
title="cocos2d-x-3.8rc0">cocos2d-x-3.8rc0</a>
</div>
<div class="select-menu-item js-navigation-item ">
<span class="select-menu-item-icon octicon octicon-check"></span>
<a href="/cocos2d/cocos2d-x/tree/cocos2d-x-3.8beta0/docs/CODING_ST
YLE.md"
data-name="cocos2d-x-3.8beta0"
data-skip-pjax="true"
rel="nofollow"
class="js-navigation-open select-menu-item-text css-truncate-ta
rget"
title="cocos2d-x-3.8beta0">cocos2d-x-3.8beta0</a>
</div>
<div class="select-menu-item js-navigation-item ">
<span class="select-menu-item-icon octicon octicon-check"></span>
<a href="/cocos2d/cocos2d-x/tree/cocos2d-x-3.7rc1/docs/CODING_STYL
E.md"
data-name="cocos2d-x-3.7rc1"
data-skip-pjax="true"
rel="nofollow"
class="js-navigation-open select-menu-item-text css-truncate-ta
rget"
title="cocos2d-x-3.7rc1">cocos2d-x-3.7rc1</a>
</div>
<div class="select-menu-item js-navigation-item ">
<span class="select-menu-item-icon octicon octicon-check"></span>
<a href="/cocos2d/cocos2d-x/tree/cocos2d-x-3.7rc0/docs/CODING_STYL
E.md"
data-name="cocos2d-x-3.7rc0"
data-skip-pjax="true"
rel="nofollow"
class="js-navigation-open select-menu-item-text css-truncate-ta
rget"
title="cocos2d-x-3.7rc0">cocos2d-x-3.7rc0</a>
</div>
<div class="select-menu-item js-navigation-item ">
<span class="select-menu-item-icon octicon octicon-check"></span>
<a href="/cocos2d/cocos2d-x/tree/cocos2d-x-3.7beta0/docs/CODING_ST
YLE.md"
data-name="cocos2d-x-3.7beta0"
data-skip-pjax="true"
rel="nofollow"
class="js-navigation-open select-menu-item-text css-truncate-ta
rget"
title="cocos2d-x-3.7beta0">cocos2d-x-3.7beta0</a>
</div>
<div class="select-menu-item js-navigation-item ">
<span class="select-menu-item-icon octicon octicon-check"></span>
<a href="/cocos2d/cocos2d-x/tree/cocos2d-x-3.7.1/docs/CODING_STYLE
.md"
data-name="cocos2d-x-3.7.1"
data-skip-pjax="true"
rel="nofollow"
class="js-navigation-open select-menu-item-text css-truncate-ta
rget"
title="cocos2d-x-3.7.1">cocos2d-x-3.7.1</a>
</div>
data-skip-pjax="true"
rel="nofollow"
class="js-navigation-open select-menu-item-text css-truncate-ta
rget"
title="cocos2d-x-3.5rc0">cocos2d-x-3.5rc0</a>
</div>
<div class="select-menu-item js-navigation-item ">
<span class="select-menu-item-icon octicon octicon-check"></span>
<a href="/cocos2d/cocos2d-x/tree/cocos2d-x-3.5beta0/docs/CODING_ST
YLE.md"
data-name="cocos2d-x-3.5beta0"
data-skip-pjax="true"
rel="nofollow"
class="js-navigation-open select-menu-item-text css-truncate-ta
rget"
title="cocos2d-x-3.5beta0">cocos2d-x-3.5beta0</a>
</div>
<div class="select-menu-item js-navigation-item ">
<span class="select-menu-item-icon octicon octicon-check"></span>
<a href="/cocos2d/cocos2d-x/tree/cocos2d-x-3.5/docs/CODING_STYLE.m
d"
data-name="cocos2d-x-3.5"
data-skip-pjax="true"
rel="nofollow"
class="js-navigation-open select-menu-item-text css-truncate-ta
rget"
title="cocos2d-x-3.5">cocos2d-x-3.5</a>
</div>
<div class="select-menu-item js-navigation-item ">
<span class="select-menu-item-icon octicon octicon-check"></span>
<a href="/cocos2d/cocos2d-x/tree/cocos2d-x-3.4rc1/docs/CODING_STYL
E.md"
data-name="cocos2d-x-3.4rc1"
data-skip-pjax="true"
rel="nofollow"
class="js-navigation-open select-menu-item-text css-truncate-ta
rget"
title="cocos2d-x-3.4rc1">cocos2d-x-3.4rc1</a>
</div>
<div class="select-menu-item js-navigation-item ">
<span class="select-menu-item-icon octicon octicon-check"></span>
<a href="/cocos2d/cocos2d-x/tree/cocos2d-x-3.4rc0/docs/CODING_STYL
E.md"
data-name="cocos2d-x-3.4rc0"
data-skip-pjax="true"
rel="nofollow"
class="js-navigation-open select-menu-item-text css-truncate-ta
rget"
title="cocos2d-x-3.4rc0">cocos2d-x-3.4rc0</a>
</div>
<div class="select-menu-item js-navigation-item ">
<span class="select-menu-item-icon octicon octicon-check"></span>
<a href="/cocos2d/cocos2d-x/tree/cocos2d-x-3.4beta0/docs/CODING_ST
YLE.md"
data-name="cocos2d-x-3.4beta0"
data-skip-pjax="true"
rel="nofollow"
class="js-navigation-open select-menu-item-text css-truncate-ta
rget"
title="cocos2d-x-3.4beta0">cocos2d-x-3.4beta0</a>
</div>
<div class="select-menu-item js-navigation-item ">
<span class="select-menu-item-icon octicon octicon-check"></span>
<a href="/cocos2d/cocos2d-x/tree/cocos2d-x-3.4/docs/CODING_STYLE.m
d"
data-name="cocos2d-x-3.4"
data-skip-pjax="true"
rel="nofollow"
class="js-navigation-open select-menu-item-text css-truncate-ta
rget"
title="cocos2d-x-3.4">cocos2d-x-3.4</a>
</div>
<div class="select-menu-item js-navigation-item ">
<span class="select-menu-item-icon octicon octicon-check"></span>
<a href="/cocos2d/cocos2d-x/tree/cocos2d-x-3.3rc2/docs/CODING_STYL
E.md"
data-name="cocos2d-x-3.3rc2"
data-skip-pjax="true"
rel="nofollow"
class="js-navigation-open select-menu-item-text css-truncate-ta
rget"
title="cocos2d-x-3.3rc2">cocos2d-x-3.3rc2</a>
</div>
<div class="select-menu-item js-navigation-item ">
<span class="select-menu-item-icon octicon octicon-check"></span>
<a href="/cocos2d/cocos2d-x/tree/cocos2d-x-3.3rc1/docs/CODING_STYL
E.md"
data-name="cocos2d-x-3.3rc1"
data-skip-pjax="true"
rel="nofollow"
class="js-navigation-open select-menu-item-text css-truncate-ta
rget"
title="cocos2d-x-3.3rc1">cocos2d-x-3.3rc1</a>
</div>
<div class="select-menu-item js-navigation-item ">
<span class="select-menu-item-icon octicon octicon-check"></span>
<a href="/cocos2d/cocos2d-x/tree/cocos2d-x-3.3rc0/docs/CODING_STYL
E.md"
data-name="cocos2d-x-3.3rc0"
data-skip-pjax="true"
rel="nofollow"
class="js-navigation-open select-menu-item-text css-truncate-ta
rget"
title="cocos2d-x-3.3rc0">cocos2d-x-3.3rc0</a>
</div>
<div class="select-menu-item js-navigation-item ">
<span class="select-menu-item-icon octicon octicon-check"></span>
<a href="/cocos2d/cocos2d-x/tree/cocos2d-x-3.3beta0/docs/CODING_ST
YLE.md"
data-name="cocos2d-x-3.3beta0"
data-skip-pjax="true"
rel="nofollow"
class="js-navigation-open select-menu-item-text css-truncate-ta
rget"
title="cocos2d-x-3.3beta0">cocos2d-x-3.3beta0</a>
</div>
<div class="select-menu-item js-navigation-item ">
<span class="select-menu-item-icon octicon octicon-check"></span>
<a href="/cocos2d/cocos2d-x/tree/cocos2d-x-3.3alpha0/docs/CODING_S
TYLE.md"
data-name="cocos2d-x-3.3alpha0"
data-skip-pjax="true"
rel="nofollow"
class="js-navigation-open select-menu-item-text css-truncate-ta
rget"
title="cocos2d-x-3.3alpha0">cocos2d-x-3.3alpha0</a>
</div>
<div class="select-menu-item js-navigation-item ">
<span class="select-menu-item-icon octicon octicon-check"></span>
<a href="/cocos2d/cocos2d-x/tree/cocos2d-x-3.3/docs/CODING_STYLE.m
d"
data-name="cocos2d-x-3.3"
data-skip-pjax="true"
rel="nofollow"
class="js-navigation-open select-menu-item-text css-truncate-ta
rget"
title="cocos2d-x-3.3">cocos2d-x-3.3</a>
</div>
<div class="select-menu-item js-navigation-item ">
<span class="select-menu-item-icon octicon octicon-check"></span>
<a href="/cocos2d/cocos2d-x/tree/cocos2d-x-3.2rc0/docs/CODING_STYL
E.md"
data-name="cocos2d-x-3.2rc0"
data-skip-pjax="true"
rel="nofollow"
class="js-navigation-open select-menu-item-text css-truncate-ta
rget"
title="cocos2d-x-3.2rc0">cocos2d-x-3.2rc0</a>
</div>
<div class="select-menu-item js-navigation-item ">
<span class="select-menu-item-icon octicon octicon-check"></span>
<a href="/cocos2d/cocos2d-x/tree/cocos2d-x-3.2alpha0/docs/CODING_S
TYLE.md"
data-name="cocos2d-x-3.2alpha0"
data-skip-pjax="true"
rel="nofollow"
class="js-navigation-open select-menu-item-text css-truncate-ta
rget"
title="cocos2d-x-3.2alpha0">cocos2d-x-3.2alpha0</a>
</div>
<div class="select-menu-item js-navigation-item ">
<span class="select-menu-item-icon octicon octicon-check"></span>
<a href="/cocos2d/cocos2d-x/tree/cocos2d-x-3.2/docs/CODING_STYLE.m
d"
data-name="cocos2d-x-3.2"
data-skip-pjax="true"
rel="nofollow"
class="js-navigation-open select-menu-item-text css-truncate-ta
rget"
title="cocos2d-x-3.2">cocos2d-x-3.2</a>
</div>
<div class="select-menu-item js-navigation-item ">
<span class="select-menu-item-icon octicon octicon-check"></span>
<a href="/cocos2d/cocos2d-x/tree/cocos2d-x-3.1rc0/docs/CODING_STYL
E.md"
data-name="cocos2d-x-3.1rc0"
data-skip-pjax="true"
rel="nofollow"
class="js-navigation-open select-menu-item-text css-truncate-ta
rget"
title="cocos2d-x-3.1rc0">cocos2d-x-3.1rc0</a>
</div>
<div class="select-menu-item js-navigation-item ">
<span class="select-menu-item-icon octicon octicon-check"></span>
<a href="/cocos2d/cocos2d-x/tree/cocos2d-x-3.1alpha1/docs/CODING_S
TYLE.md"
data-name="cocos2d-x-3.1alpha1"
data-skip-pjax="true"
rel="nofollow"
class="js-navigation-open select-menu-item-text css-truncate-ta
rget"
title="cocos2d-x-3.1alpha1">cocos2d-x-3.1alpha1</a>
</div>
<div class="select-menu-item js-navigation-item ">
<span class="select-menu-item-icon octicon octicon-check"></span>
<a href="/cocos2d/cocos2d-x/tree/cocos2d-x-3.1alpha0/docs/CODING_S
TYLE.md"
data-name="cocos2d-x-3.1alpha0"
data-skip-pjax="true"
rel="nofollow"
class="js-navigation-open select-menu-item-text css-truncate-ta
rget"
title="cocos2d-x-3.1alpha0">cocos2d-x-3.1alpha0</a>
</div>
<div class="select-menu-item js-navigation-item ">
<span class="select-menu-item-icon octicon octicon-check"></span>
<a href="/cocos2d/cocos2d-x/tree/cocos2d-x-3.1.1/docs/CODING_STYLE
.md"
data-name="cocos2d-x-3.1.1"
data-skip-pjax="true"
rel="nofollow"
class="js-navigation-open select-menu-item-text css-truncate-ta
rget"
title="cocos2d-x-3.1.1">cocos2d-x-3.1.1</a>
</div>
<div class="select-menu-item js-navigation-item ">
<span class="select-menu-item-icon octicon octicon-check"></span>
<a href="/cocos2d/cocos2d-x/tree/cocos2d-x-3.1/docs/CODING_STYLE.m
d"
data-name="cocos2d-x-3.1"
data-skip-pjax="true"
rel="nofollow"
class="js-navigation-open select-menu-item-text css-truncate-ta
rget"
title="cocos2d-x-3.1">cocos2d-x-3.1</a>
</div>
<div class="select-menu-item js-navigation-item ">
<span class="select-menu-item-icon octicon octicon-check"></span>
<a href="/cocos2d/cocos2d-x/tree/cocos2d-x-3.0rc2/docs/CODING_STYL
E.md"
data-name="cocos2d-x-3.0rc2"
data-skip-pjax="true"
rel="nofollow"
class="js-navigation-open select-menu-item-text css-truncate-ta
rget"
title="cocos2d-x-3.0rc2">cocos2d-x-3.0rc2</a>
</div>
<div class="select-menu-item js-navigation-item ">
<span class="select-menu-item-icon octicon octicon-check"></span>
<a href="/cocos2d/cocos2d-x/tree/cocos2d-x-3.0rc1/docs/CODING_STYL
E.md"
data-name="cocos2d-x-3.0rc1"
data-skip-pjax="true"
rel="nofollow"
class="js-navigation-open select-menu-item-text css-truncate-ta
rget"
title="cocos2d-x-3.0rc1">cocos2d-x-3.0rc1</a>
</div>
<div class="select-menu-item js-navigation-item ">
<span class="select-menu-item-icon octicon octicon-check"></span>
<a href="/cocos2d/cocos2d-x/tree/cocos2d-x-3.0rc0/docs/CODING_STYL
E.md"
data-name="cocos2d-x-3.0rc0"
data-skip-pjax="true"
rel="nofollow"
class="js-navigation-open select-menu-item-text css-truncate-ta
rget"
title="cocos2d-x-3.0rc0">cocos2d-x-3.0rc0</a>
</div>
<div class="select-menu-item js-navigation-item ">
<span class="select-menu-item-icon octicon octicon-check"></span>
<a href="/cocos2d/cocos2d-x/tree/cocos2d-x-3.0rc/docs/CODING_STYLE
.md"
data-name="cocos2d-x-3.0rc"
data-skip-pjax="true"
rel="nofollow"
class="js-navigation-open select-menu-item-text css-truncate-ta
rget"
title="cocos2d-x-3.0rc">cocos2d-x-3.0rc</a>
</div>
<div class="select-menu-item js-navigation-item ">
<span class="select-menu-item-icon octicon octicon-check"></span>
<a href="/cocos2d/cocos2d-x/tree/cocos2d-x-3.0beta2/docs/CODING_ST
YLE.md"
data-name="cocos2d-x-3.0beta2"
data-skip-pjax="true"
rel="nofollow"
class="js-navigation-open select-menu-item-text css-truncate-ta
rget"
title="cocos2d-x-3.0beta2">cocos2d-x-3.0beta2</a>
</div>
<div class="select-menu-item js-navigation-item ">
<span class="select-menu-item-icon octicon octicon-check"></span>
<a href="/cocos2d/cocos2d-x/tree/cocos2d-x-3.0beta/docs/CODING_STY
LE.md"
data-name="cocos2d-x-3.0beta"
data-skip-pjax="true"
rel="nofollow"
class="js-navigation-open select-menu-item-text css-truncate-ta
rget"
title="cocos2d-x-3.0beta">cocos2d-x-3.0beta</a>
</div>
<div class="select-menu-item js-navigation-item ">
<span class="select-menu-item-icon octicon octicon-check"></span>
<a href="/cocos2d/cocos2d-x/tree/cocos2d-x-3.0alpha1/docs/CODING_S
TYLE.md"
data-name="cocos2d-x-3.0alpha1"
data-skip-pjax="true"
rel="nofollow"
class="js-navigation-open select-menu-item-text css-truncate-ta
rget"
title="cocos2d-x-3.0alpha1">cocos2d-x-3.0alpha1</a>
</div>
<div class="select-menu-item js-navigation-item ">
<span class="select-menu-item-icon octicon octicon-check"></span>
<a href="/cocos2d/cocos2d-x/tree/cocos2d-x-3.0alpha0-pre/docs/CODI
NG_STYLE.md"
data-name="cocos2d-x-3.0alpha0-pre"
data-skip-pjax="true"
rel="nofollow"
class="js-navigation-open select-menu-item-text css-truncate-ta
rget"
title="cocos2d-x-3.0alpha0-pre">cocos2d-x-3.0alpha0-pre</a>
</div>
<div class="select-menu-item js-navigation-item ">
<span class="select-menu-item-icon octicon octicon-check"></span>
<a href="/cocos2d/cocos2d-x/tree/cocos2d-x-3.0alpha0/docs/CODING_S
TYLE.md"
data-name="cocos2d-x-3.0alpha0"
data-skip-pjax="true"
rel="nofollow"
class="js-navigation-open select-menu-item-text css-truncate-ta
rget"
title="cocos2d-x-3.0alpha0">cocos2d-x-3.0alpha0</a>
</div>
<div class="select-menu-item js-navigation-item ">
<span class="select-menu-item-icon octicon octicon-check"></span>
<a href="/cocos2d/cocos2d-x/tree/cocos2d-x-3.0/docs/CODING_STYLE.m
d"
data-name="cocos2d-x-3.0"
data-skip-pjax="true"
rel="nofollow"
class="js-navigation-open select-menu-item-text css-truncate-ta
rget"
title="cocos2d-x-3.0">cocos2d-x-3.0</a>
</div>
<div class="select-menu-item js-navigation-item ">
<span class="select-menu-item-icon octicon octicon-check"></span>
<a href="/cocos2d/cocos2d-x/tree/cocos2d-x-2.2.6/docs/CODING_STYLE
.md"
data-name="cocos2d-x-2.2.6"
data-skip-pjax="true"
rel="nofollow"
class="js-navigation-open select-menu-item-text css-truncate-ta
rget"
title="cocos2d-x-2.2.6">cocos2d-x-2.2.6</a>
</div>
<div class="select-menu-item js-navigation-item ">
<span class="select-menu-item-icon octicon octicon-check"></span>
<a href="/cocos2d/cocos2d-x/tree/cocos2d-x-2.2.5/docs/CODING_STYLE
.md"
data-name="cocos2d-x-2.2.5"
data-skip-pjax="true"
rel="nofollow"
class="js-navigation-open select-menu-item-text css-truncate-ta
rget"
title="cocos2d-x-2.2.5">cocos2d-x-2.2.5</a>
</div>
<div class="select-menu-item js-navigation-item ">
<span class="select-menu-item-icon octicon octicon-check"></span>
<a href="/cocos2d/cocos2d-x/tree/cocos2d-x-2.2.4/docs/CODING_STYLE
.md"
data-name="cocos2d-x-2.2.4"
data-skip-pjax="true"
rel="nofollow"
class="js-navigation-open select-menu-item-text css-truncate-ta
rget"
title="cocos2d-x-2.2.4">cocos2d-x-2.2.4</a>
</div>
<div class="select-menu-item js-navigation-item ">
<span class="select-menu-item-icon octicon octicon-check"></span>
<a href="/cocos2d/cocos2d-x/tree/cocos2d-x-2.2.3/docs/CODING_STYLE
.md"
data-name="cocos2d-x-2.2.3"
data-skip-pjax="true"
rel="nofollow"
class="js-navigation-open select-menu-item-text css-truncate-ta
rget"
title="cocos2d-x-2.2.3">cocos2d-x-2.2.3</a>
</div>
<div class="select-menu-item js-navigation-item ">
<span class="select-menu-item-icon octicon octicon-check"></span>
<a href="/cocos2d/cocos2d-x/tree/cocos2d-x-2.2.2/docs/CODING_STYLE
.md"
data-name="cocos2d-x-2.2.2"
data-skip-pjax="true"
rel="nofollow"
class="js-navigation-open select-menu-item-text css-truncate-ta
rget"
title="cocos2d-x-2.2.2">cocos2d-x-2.2.2</a>
</div>
<div class="select-menu-item js-navigation-item ">
<span class="select-menu-item-icon octicon octicon-check"></span>
<a href="/cocos2d/cocos2d-x/tree/cocos2d-x-2.2.1/docs/CODING_STYLE
.md"
data-name="cocos2d-x-2.2.1"
data-skip-pjax="true"
rel="nofollow"
class="js-navigation-open select-menu-item-text css-truncate-ta
rget"
title="cocos2d-x-2.2.1">cocos2d-x-2.2.1</a>
</div>
<div class="select-menu-item js-navigation-item ">
<span class="select-menu-item-icon octicon octicon-check"></span>
<a href="/cocos2d/cocos2d-x/tree/cocos2d-x-2.2/docs/CODING_STYLE.m
d"
data-name="cocos2d-x-2.2"
data-skip-pjax="true"
rel="nofollow"
class="js-navigation-open select-menu-item-text css-truncate-ta
rget"
title="cocos2d-x-2.2">cocos2d-x-2.2</a>
</div>
<div class="select-menu-item js-navigation-item ">
<span class="select-menu-item-icon octicon octicon-check"></span>
<a href="/cocos2d/cocos2d-x/tree/cocos2d-x-2.1.5/docs/CODING_STYLE
.md"
data-name="cocos2d-x-2.1.5"
data-skip-pjax="true"
rel="nofollow"
rel="nofollow"
class="js-navigation-open select-menu-item-text css-truncate-ta
rget"
title="cocos2d-1.0.1-x-0.13.0-beta">cocos2d-1.0.1-x-0.13.0-beta
</a>
</div>
<div class="select-menu-item js-navigation-item ">
<span class="select-menu-item-icon octicon octicon-check"></span>
<a href="/cocos2d/cocos2d-x/tree/cocos2d-1.0.1-x-0.12.0/docs/CODIN
G_STYLE.md"
data-name="cocos2d-1.0.1-x-0.12.0"
data-skip-pjax="true"
rel="nofollow"
class="js-navigation-open select-menu-item-text css-truncate-ta
rget"
title="cocos2d-1.0.1-x-0.12.0">cocos2d-1.0.1-x-0.12.0</a>
</div>
<div class="select-menu-item js-navigation-item ">
<span class="select-menu-item-icon octicon octicon-check"></span>
<a href="/cocos2d/cocos2d-x/tree/cocos2d-1.0.1-x-0.11.0/docs/CODIN
G_STYLE.md"
data-name="cocos2d-1.0.1-x-0.11.0"
data-skip-pjax="true"
rel="nofollow"
class="js-navigation-open select-menu-item-text css-truncate-ta
rget"
title="cocos2d-1.0.1-x-0.11.0">cocos2d-1.0.1-x-0.11.0</a>
</div>
<div class="select-menu-item js-navigation-item ">
<span class="select-menu-item-icon octicon octicon-check"></span>
<a href="/cocos2d/cocos2d-x/tree/cocos2d-1.0.1-x-0.10.0/docs/CODIN
G_STYLE.md"
data-name="cocos2d-1.0.1-x-0.10.0"
data-skip-pjax="true"
rel="nofollow"
class="js-navigation-open select-menu-item-text css-truncate-ta
rget"
title="cocos2d-1.0.1-x-0.10.0">cocos2d-1.0.1-x-0.10.0</a>
</div>
<div class="select-menu-item js-navigation-item ">
<span class="select-menu-item-icon octicon octicon-check"></span>
<a href="/cocos2d/cocos2d-x/tree/cocos2d-1.0.1-x-0.9.2/docs/CODING
_STYLE.md"
data-name="cocos2d-1.0.1-x-0.9.2"
data-skip-pjax="true"
rel="nofollow"
class="js-navigation-open select-menu-item-text css-truncate-ta
rget"
title="cocos2d-1.0.1-x-0.9.2">cocos2d-1.0.1-x-0.9.2</a>
</div>
<div class="select-menu-item js-navigation-item ">
<span class="select-menu-item-icon octicon octicon-check"></span>
<a href="/cocos2d/cocos2d-x/tree/cocos2d-1.0.1-x-0.9.1/docs/CODING
_STYLE.md"
data-name="cocos2d-1.0.1-x-0.9.1"
data-skip-pjax="true"
rel="nofollow"
class="js-navigation-open select-menu-item-text css-truncate-ta
rget"
title="cocos2d-1.0.1-x-0.9.1">cocos2d-1.0.1-x-0.9.1</a>
</div>
<div class="select-menu-item js-navigation-item ">
<span class="select-menu-item-icon octicon octicon-check"></span>
<a href="/cocos2d/cocos2d-x/tree/cocos2d-1.0.0-x-0.9.0/docs/CODING
_STYLE.md"
data-name="cocos2d-1.0.0-x-0.9.0"
data-skip-pjax="true"
rel="nofollow"
class="js-navigation-open select-menu-item-text css-truncate-ta
rget"
title="cocos2d-1.0.0-x-0.9.0">cocos2d-1.0.0-x-0.9.0</a>
</div>
<div class="select-menu-item js-navigation-item ">
<span class="select-menu-item-icon octicon octicon-check"></span>
<a href="/cocos2d/cocos2d-x/tree/cocos2d-0.99.5-x-0.8.5/docs/CODIN
G_STYLE.md"
data-name="cocos2d-0.99.5-x-0.8.5"
data-skip-pjax="true"
rel="nofollow"
class="js-navigation-open select-menu-item-text css-truncate-ta
rget"
title="cocos2d-0.99.5-x-0.8.5">cocos2d-0.99.5-x-0.8.5</a>
</div>
<div class="select-menu-item js-navigation-item ">
<span class="select-menu-item-icon octicon octicon-check"></span>
<a href="/cocos2d/cocos2d-x/tree/cocos2d-0.99.5-x-0.8.4/docs/CODIN
G_STYLE.md"
data-name="cocos2d-0.99.5-x-0.8.4"
data-skip-pjax="true"
rel="nofollow"
class="js-navigation-open select-menu-item-text css-truncate-ta
rget"
title="cocos2d-0.99.5-x-0.8.4">cocos2d-0.99.5-x-0.8.4</a>
</div>
<div class="select-menu-item js-navigation-item ">
<span class="select-menu-item-icon octicon octicon-check"></span>
<a href="/cocos2d/cocos2d-x/tree/cocos2d-0.99.5-x-0.8.3/docs/CODIN
G_STYLE.md"
data-name="cocos2d-0.99.5-x-0.8.3"
data-skip-pjax="true"
rel="nofollow"
class="js-navigation-open select-menu-item-text css-truncate-ta
rget"
title="cocos2d-0.99.5-x-0.8.3">cocos2d-0.99.5-x-0.8.3</a>
</div>
<div class="select-menu-item js-navigation-item ">
<span class="select-menu-item-icon octicon octicon-check"></span>
<a href="/cocos2d/cocos2d-x/tree/cocos2d-0.99.5-x-0.8.2/docs/CODIN
G_STYLE.md"
data-name="cocos2d-0.99.5-x-0.8.2"
data-skip-pjax="true"
rel="nofollow"
class="js-navigation-open select-menu-item-text css-truncate-ta
rget"
title="cocos2d-0.99.5-x-0.8.2">cocos2d-0.99.5-x-0.8.2</a>
</div>
<div class="select-menu-item js-navigation-item ">
<span class="select-menu-item-icon octicon octicon-check"></span>
<a href="/cocos2d/cocos2d-x/tree/cocos2d-0.99.5-x-0.8.1/docs/CODIN
G_STYLE.md"
data-name="cocos2d-0.99.5-x-0.8.1"
data-skip-pjax="true"
rel="nofollow"
class="js-navigation-open select-menu-item-text css-truncate-ta
rget"
title="cocos2d-0.99.5-x-0.8.1">cocos2d-0.99.5-x-0.8.1</a>
</div>
<div class="select-menu-item js-navigation-item ">
<span class="select-menu-item-icon octicon octicon-check"></span>
<a href="/cocos2d/cocos2d-x/tree/cocos2d-0.99.5-x-0.8.0/docs/CODIN
G_STYLE.md"
data-name="cocos2d-0.99.5-x-0.8.0"
data-skip-pjax="true"
rel="nofollow"
class="js-navigation-open select-menu-item-text css-truncate-ta
rget"
title="cocos2d-0.99.5-x-0.8.0">cocos2d-0.99.5-x-0.8.0</a>
</div>
<div class="select-menu-item js-navigation-item ">
<span class="select-menu-item-icon octicon octicon-check"></span>
<a href="/cocos2d/cocos2d-x/tree/cocos2d-0.99.5-x-0.7.2/docs/CODIN
G_STYLE.md"
data-name="cocos2d-0.99.5-x-0.7.2"
data-skip-pjax="true"
rel="nofollow"
class="js-navigation-open select-menu-item-text css-truncate-ta
rget"
title="cocos2d-0.99.5-x-0.7.2">cocos2d-0.99.5-x-0.7.2</a>
</div>
<div class="select-menu-item js-navigation-item ">
<span class="select-menu-item-icon octicon octicon-check"></span>
<a href="/cocos2d/cocos2d-x/tree/cocos2d-0.99.4-x-0.7.1/docs/CODIN
G_STYLE.md"
data-name="cocos2d-0.99.4-x-0.7.1"
data-skip-pjax="true"
rel="nofollow"
class="js-navigation-open select-menu-item-text css-truncate-ta
rget"
title="cocos2d-0.99.4-x-0.7.1">cocos2d-0.99.4-x-0.7.1</a>
</div>
<div class="select-menu-item js-navigation-item ">
<span class="select-menu-item-icon octicon octicon-check"></span>
<a href="/cocos2d/cocos2d-x/tree/cocos2d-0.99.4-x-0.7.0/docs/CODIN
G_STYLE.md"
data-name="cocos2d-0.99.4-x-0.7.0"
data-skip-pjax="true"
rel="nofollow"
class="js-navigation-open select-menu-item-text css-truncate-ta
rget"
title="cocos2d-0.99.4-x-0.7.0">cocos2d-0.99.4-x-0.7.0</a>
</div>
</div>
<div class="select-menu-no-results">Nothing to show</div>
</div>
</div>
</div>
</div>
</div>
<div class="file-info">
2850 lines (1941 sloc)
<span class="file-info-divider"></span>
146.751 kB
</div>
</div>
<div id="readme" class="blob instapaper_body">
<article class="markdown-body entry-content" itemprop="mainContentOfPage"><h
1><a id="user-content--c-coding-style" class="anchor" href="#-c-coding-style" ar
ia-hidden="true"><span class="octicon octicon-link"></span></a><a href="https://
camo.githubusercontent.com/a3c5ddb210347ee98404094f5a0ae3f707453efe/68747470733a
2f2f6c68332e676f6f676c6575736572636f6e74656e742e636f6d2f2d676c77777a6d4679556d6b
2f555167506e6c78343075492f4141414141414141727a672f5750525731306b6b65634d2f733134
342f636f636f7332642d782d6c6f676f2e706e67" target="_blank"><img src="https://camo
.githubusercontent.com/a3c5ddb210347ee98404094f5a0ae3f707453efe/68747470733a2f2f
6c68332e676f6f676c6575736572636f6e74656e742e636f6d2f2d676c77777a6d4679556d6b2f55
5167506e6c78343075492f4141414141414141727a672f5750525731306b6b65634d2f733134342f
636f636f7332642d782d6c6f676f2e706e67" alt="" data-canonical-src="https://lh3.goo
gleusercontent.com/-glwwzmFyUmk/UQgPnlx40uI/AAAAAAAArzg/WPRW10kkecM/s144/cocos2d
-x-logo.png" style="max-width:100%;"></a> C++ coding style</h1>
<p><em>v0.2 - Last updated November 8, 2013</em></p>
<p><em>Forked from <a href="http://google-styleguide.googlecode.com/svn/trunk/cp
pguide.xml">Google's C++ coding style</a> rev. 3.274</em></p>
<p><strong>Table of Contents</strong> <em>generated with <a href="http://doctoc
.herokuapp.com/">DocToc</a></em></p>
<ul>
<li><a href="#header-files">Header Files</a>
<ul>
<li><a href="#the-define-guard">The #define Guard</a></li>
<li><a href="#forward-declarations">Forward Declarations</a></li>
<li><a href="#inline-functions">Inline Functions</a></li>
<li><a href="#the--inlh-files">The -inl.h Files</a></li>
<li><a href="#function-parameter-ordering">Function Parameter Ordering</a></li>
<li><a href="#names-and-order-of-includes">Names and Order of Includes</a></li>
</ul></li>
<li><a href="#scoping">Scoping</a>
<ul>
<li><a href="#namespaces">Namespaces</a>
<ul>
<li><a href="#unnamed-namespaces">Unnamed Namespaces</a></li>
<li><a href="#named-namespaces">Named Namespaces</a></li>
</ul></li>
<li><a href="#nested-classes">Nested Classes</a></li>
<li><a href="#nonmember-static-member-and-global-functions">Nonmember, Static Me
mber, and Global Functions</a></li>
<li><a href="#local-variables">Local Variables</a></li>
<li><a href="#static-and-global-variables">Static and Global Variables</a></li>
</ul></li>
<li><a href="#classes">Classes</a>
<ul>
<li><a href="#doing-work-in-constructors">Doing Work in Constructors</a></li>
<li><a href="#initialization">Initialization</a></li>
<li><a href="#explicit-constructors">Explicit Constructors</a></li>
<li><a href="#copy-constructors">Copy Constructors</a></li>
<li><a href="#delegating-and-inheriting-constructors">Delegating and inheriting
constructors</a></li>
<li><a href="#structs-vs-classes">Structs vs. Classes</a></li>
<li><a href="#inheritance">Inheritance</a></li>
<li><a href="#multiple-inheritance">Multiple Inheritance</a></li>
<li><a href="#interfaces">Interfaces</a></li>
<li><a href="#operator-overloading">Operator Overloading</a></li>
<li><a href="#access-control">Access Control</a></li>
<li><a href="#declaration-order">Declaration Order</a></li>
<li><a href="#write-short-functions">Write Short Functions</a></li>
</ul></li>
<li><a href="#other-c-features">Other C++ Features</a>
<ul>
<li><a href="#ownership-and-smart-pointers">Ownership and Smart Pointers</a></li
>
<li><a href="#reference-arguments">Reference Arguments</a></li>
<li><a href="#rvalue-references">Rvalue references</a></li>
<li><a href="#function-overloading">Function Overloading</a></li>
<li><a href="#default-arguments">Default Arguments</a></li>
<li><a href="#variable-length-arrays-and-alloca">Variable-Length Arrays and allo
ca()</a></li>
<li><a href="#friends">Friends</a></li>
<li><a href="#exceptions">Exceptions</a></li>
<li><a href="#run-time-type-information-rtti">Run-Time Type Information (RTTI)</
a></li>
<li><a href="#casting">Casting</a></li>
<li><a href="#streams">Streams</a></li>
<li><a href="#preincrement-and-predecrement">Preincrement and Predecrement</a></
li>
<li><a href="#use-of-const">Use of const</a></li>
<li><a href="#use-of-constexpr">Use of constexpr</a></li>
<li><a href="#use-of-constexpr-1">Use of constexpr</a></li>
<li><a href="#integer-types">Integer Types</a>
<ul>
<li><a href="#on-unsigned-integers">On Unsigned Integers</a></li>
</ul></li>
<li><a href="#64-bit-portability">64-bit Portability</a></li>
<li><a href="#preprocessor-macros">Preprocessor Macros</a></li>
<li><a href="#0-and-nullptrnull">0 and nullptr/NULL</a></li>
<li><a href="#sizeof">sizeof</a></li>
<li><a href="#auto">auto</a></li>
<li><a href="#brace-initialization">Brace Initialization</a></li>
<li><a href="#lambda-expressions">Lambda expressions</a></li>
<li><a href="#boost">Boost</a></li>
<li><a href="#c11">C++11</a></li>
<li><a href="#general-naming-rules">General Naming Rules</a></li>
<li><a href="#file-names">File Names</a></li>
<li><a href="#type-names">Type Names</a></li>
<li><a href="#variable-names">Variable Names</a>
<ul>
<li><a href="#common-variable-names">Common Variable names</a></li>
<li><a href="#class-data-members">Class Data Members</a></li>
<li><a href="#struct-variables">Struct
<li><a href="#global-variables">Global
</ul></li>
<li><a href="#constant-names">Constant
<li><a href="#function-names">Function
Variables</a></li>
Variables</a></li>
Names</a></li>
Names</a>
<ul>
<li><a href="#regular-functions">Regular Functions</a></li>
<li><a href="#accessors-and-mutators">Accessors and Mutators</a></li>
</ul></li>
<li><a href="#namespace-names">Namespace Names</a></li>
<li><a href="#enumerator-names">Enumerator Names</a></li>
<li><a href="#macro-names">Macro Names</a></li>
<li><a href="#exceptions-to-naming-rules">Exceptions to Naming Rules</a>
<ul>
<li><a href="#bigopen">bigopen()</a></li>
<li><a href="#uint">uint</a></li>
<li><a href="#bigpos">bigpos</a></li>
<li><a href="#sparse_hash_map">sparse_hash_map</a></li>
<li><a href="#longlong_max">LONGLONG_MAX</a></li>
</ul></li>
</ul></li>
<li><a href="#comments">Comments</a>
<ul>
<li><a href="#doxygen">Doxygen</a></li>
<li><a href="#comment-style">Comment Style</a></li>
<li><a href="#file-comments">File Comments</a>
<ul>
<li><a href="#legal-notice-and-author-line">Legal Notice and Author Line</a></li
>
<li><a href="#file-contents">File Contents</a></li>
</ul></li>
<li><a href="#class-comments">Class Comments</a></li>
<li><a href="#function-comments">Function Comments</a>
<ul>
<li><a href="#function-declarations">Function Declarations</a></li>
<li><a href="#function-definitions">Function Definitions</a></li>
</ul></li>
<li><a href="#variable-comments">Variable Comments</a>
<ul>
<li><a href="#class-data-members-1">Class Data Members</a></li>
<li><a href="#global-variables-1">Global Variables</a></li>
</ul></li>
<li><a href="#implementation-comments">Implementation Comments</a>
<ul>
<li><a href="#class-data-members-2">Class Data Members</a></li>
<li><a href="#line-comments">Line Comments</a></li>
<li><a href="#nullptrnull-truefalse-1-2-3">nullptr/NULL, true/false, 1, 2, 3...<
/a></li>
<li><a href="#donts">Don'ts</a></li>
</ul></li>
<li><a href="#punctuation-spelling-and-grammar">Punctuation, Spelling and Gramma
r</a></li>
<li><a href="#todo-comments">TODO Comments</a></li>
uard</h2>
<p>All header files should have <code>#define</code> guards to prevent multiple
inclusion. The format of the symbol name should be <code><PROJECT>_<PAT
H>_<FILE>_H_.</code></p>
<p>To guarantee uniqueness, they should be based on the full path in a project's
source tree. For example, the file <code>cocos2dx/sprites_nodes/CCSprite.h</cod
e> in project foo should have the following guard:</p>
<div class="highlight highlight-cpp"><pre>#<span class="pl-k">ifndef</span> COCO
S2DX_SPRITE_NODES_CCSPRITE_H_
#<span class="pl-k">define</span> <span class="pl-en">COCOS2DX_SPRITE_NODES_CCSP
RITE_H_</span>
...
#<span class="pl-k">endif</span> <span class="pl-c">// COCOS2DX_SPRITE_NODES_CC
SPRITE_H_</span></pre></div>
<div class="highlight highlight-cpp"><pre><span class="pl-c">// Pragma once is s
till open for debate</span>
#<span class="pl-k">pragma</span> once</pre></div>
<p>We are cosidering using <code>#pragma once</code> as well, although we haven'
t determined yet if it is supported in all our target platforms.</p>
<h2><a id="user-content-forward-declarations" class="anchor" href="#forward-decl
arations" aria-hidden="true"><span class="octicon octicon-link"></span></a>Forwa
rd Declarations</h2>
<p>You may forward declare ordinary classes in order to avoid unnecessary <code>
#includes</code>.</p>
<p><strong>Definition:</strong> A "forward declaration" is a declaration of a cl
ass, function, or template without an associated definition. #include lines can
often be replaced with forward declarations of whatever symbols are actually use
d by the client code.</p>
<p><strong>Pros:</strong></p>
<ul>
<li>Unnecessary #includes force the compiler to open more files and process more
input.</li>
<li>They can also force your code to be recompiled more often, due to changes in
the header.</li>
</ul>
<p><strong>Cons:</strong></p>
<ul>
<li>It can be difficult to determine the correct form of a forward declaration i
n the presence of features like templates, typedefs, default parameters, and usi
ng declarations.</li>
<li>It can be difficult to determine whether a forward declaration or a full #in
clude is needed for a given piece of code, particularly when implicit conversion
operations are involved. In extreme cases, replacing an #include with a forward
declaration can silently change the meaning of code.</li>
<li>Forward declaring multiple symbols from a header can be more verbose than si
mply #includeing the header.</li>
<li>Forward declarations of functions and templates can prevent the header owner
s from making otherwise-compatible changes to their APIs; for example, widening
a parameter type, or adding a template parameter with a default value.</li>
<li>Forward declaring symbols from namespace std:: usually yields undefined beha
vior.</li>
<li>Structuring code to enable forward declarations (e.g. using pointer members
instead of object members) can make the code slower and more complex.</li>
<li>The practical efficiency benefits of forward declarations are unproven.</li>
</ul>
<p><strong>Decision:</strong></p>
<ul>
<li>When using a function declared in a header file, always #include that header
.</li>
<li>When using a class template, prefer to #include its header file.</li>
<li>When using an ordinary class, relying on a forward declaration is OK, but be
wary of situations where a forward declaration may be insufficient or incorrect
; when in doubt, just #include the appropriate header.</li>
<li>Do not replace data members with pointers just to avoid an #include.</li>
</ul>
<p>Always #include the file that actually provides the declarations/definitions
you need; do not rely on the symbol being brought in transitively via headers no
t directly included. One exception is that <code>Myfile.cpp</code> may rely on #
includes and forward declarations from its corresponding header file <code>Myfil
e.h</code>.</p>
<h2><a id="user-content-inline-functions" class="anchor" href="#inline-functions
" aria-hidden="true"><span class="octicon octicon-link"></span></a>Inline Functi
ons</h2>
<p>Define functions inline only when they are small, say, 10 lines or less.</p>
<p><strong>Definition:</strong>
You can declare functions in a way that allows the compiler to expand them inlin
e rather than calling them through the usual function call mechanism.</p>
<p><strong>Pros:</strong>
Inlining a function can generate more efficient object code, as long as the inli
ned function is small. Feel free to inline accessors and mutators, and other sho
rt, performance-critical functions.</p>
<p><strong>Cons:</strong>
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 v
ery small accessor function will usually decrease code size while inlining a ver
y large function can dramatically increase code size. On modern processors small
er code usually runs faster due to better use of the instruction cache.</p>
<p><strong>Decision:</strong>
A decent rule of thumb is to not inline a function if it is more than 10 lines l
ong. Beware of destructors, which are often longer than they appear because of i
mplicit member- and base-destructor calls!</p>
<p>Another useful rule of thumb: it's typically not cost effective to inline fun
ctions with loops or switch statements (unless, in the common case, the loop or
switch statement is never executed).</p>
<p>It is important to know that functions are not always inlined even if they ar
e declared as such; for example, virtual and recursive functions are not normall
y inlined. Usually recursive functions should not be inline. The main reason for
making a virtual function inline is to place its definition in the class, eithe
r for convenience or to document its behavior, e.g., for accessors and mutators.
</p>
<h2><a id="user-content-the--inlh-files" class="anchor" href="#the--inlh-files"
aria-hidden="true"><span class="octicon octicon-link"></span></a>The -inl.h File
s</h2>
<p>You may use file names with a -inl.h suffix to define complex inline function
s when needed.</p>
<p>The definition of an inline function needs to be in a header file, so that th
e compiler has the definition available for inlining at the call sites. However,
implementation code properly belongs in .cpp files, and we do not like to have
much actual code in .h files unless there is a readability or performance advant
age.</p>
<p>If an inline function definition is short, with very little, if any, logic in
it, you should put the code in your .h file. For example, accessors and mutator
s should certainly be inside a class definition. More complex inline functions m
ay also be put in a .h file for the convenience of the implementer and callers,
though if this makes the .h file too unwieldy you can instead put that code in a
separate -inl.h file. This separates the implementation from the class definiti
on, while still allowing the implementation to be included where necessary.</p>
<p>Another use of -inl.h files is for definitions of function templates. This ca
n be used to keep your template definitions easy to read.</p>
<p>Do not forget that a -inl.h file requires a #define guard just like any other
header file.</p>
<h2><a id="user-content-function-parameter-ordering" class="anchor" href="#funct
ion-parameter-ordering" aria-hidden="true"><span class="octicon octicon-link"></
span></a>Function Parameter Ordering</h2>
<p>When defining a function, parameter order is: inputs, then outputs.</p>
<p>Parameters to C/C++ functions are either input to the function, output from t
he function, or both. Input parameters are usually <code>values</code> or <code>
const references</code>, while output and input/output parameters will be <code>
non-const pointers</code> . When ordering function parameters, put all input-onl
y parameters before any output parameters. In particular, do not add new paramet
ers to the end of the function just because they are new; place new input-only p
arameters before the output parameters.</p>
<p>This is not a hard-and-fast rule. Parameters that are both input and output (
often classes/structs) muddy the waters, and, as always, consistency with relate
d functions may require you to bend the rule.</p>
<h2><a id="user-content-names-and-order-of-includes" class="anchor" href="#names
-and-order-of-includes" aria-hidden="true"><span class="octicon octicon-link"></
span></a>Names and Order of Includes</h2>
<p>Use standard order for readability and to avoid hidden dependencies: C librar
y, C++ library, other libraries' .h, your project's .h.</p>
<p>All of a project's header files should be listed as descendants of the projec
t's source directory without use of UNIX directory shortcuts . (the current dire
class="pl-pds"><<
class="pl-pds"><<
class="pl-pds"><<
class="pl-pds"><<
<p>Use of unnamed namespaces in header files can easily cause violations of the
C++ One Definition Rule (ODR).</p>
<p><strong>Decision:</strong>
Use namespaces according to the policy described below. Terminate namespaces wit
h comments as shown in the given examples.</p>
<h3><a id="user-content-unnamed-namespaces" class="anchor" href="#unnamed-namesp
aces" aria-hidden="true"><span class="octicon octicon-link"></span></a>Unnamed N
amespaces</h3>
<p>Unnamed namespaces are allowed and even encouraged in .cpp files, to avoid ru
ntime naming conflicts:</p>
<div class="highlight highlight-cpp"><pre><span class="pl-k">namespace</span> {
<span class="pl-c">// This is in a .cpp file.</span>
<span class="pl-c">// The content of a namespace is not indented</span>
<span class="pl-k">enum</span> { UNUSED, EOF, ERROR };
<span class="pl-c
">// Commonly used tokens.</span>
<span class="pl-k">bool</span> <span class="pl-en">atEof</span>() { <span class=
"pl-k">return</span> _pos == EOF; } <span class="pl-c">// Uses our namespace's
EOF.</span>
} <span class="pl-c">// namespace</span></pre></div>
<p>However, file-scope declarations that are associated with a particular class
may be declared in that class as types, static data members or static member fun
ctions rather than as members of an unnamed namespace.
Do not use unnamed namespaces in .h files.</p>
<h3><a id="user-content-named-namespaces" class="anchor" href="#named-namespaces
" aria-hidden="true"><span class="octicon octicon-link"></span></a>Named Namespa
ces</h3>
<p>Named namespaces should be used as follows:</p>
<p>Namespaces wrap the entire source file after includes, gflags definitions/dec
larations, and forward declarations of classes from other namespaces:</p>
<div class="highlight highlight-cpp"><pre><span class="pl-c">// In the .h file</
span>
<span class="pl-c">// When using the cocos2d namespace</span>
NS_CC_BEGIN
<span class="pl-c">// All declarations are within the namespace scope.</span>
<span class="pl-c">// Notice the lack of indentation.</span>
<span class="pl-k">class</span> <span class="pl-en">MyClass</span>
{
<span class="pl-k">public:</span>
...
<span class="pl-k">void</span> <span class="pl-en">foo</span>();
};
NS_CC_END</pre></div>
<div class="highlight highlight-cpp"><pre><span class="pl-c">// In the .h file</
span>
<span class="pl-c">// When NOT using the cocos2d namespace</span>
<span class="pl-k">namespace</span> <span class="pl-en">mynamespace</span> {
A class can define another class within it; this is also called a member class.<
/p>
<div class="highlight highlight-cpp"><pre><span class="pl-k">class</span> <span
class="pl-en">Foo</span>
{
<span class="pl-k">private:</span>
<span class="pl-c">// Bar is a member class, nested within Foo.</span>
<span class="pl-k">class</span> <span class="pl-en">Bar</span>
{
...
};
};</pre></div>
<p><strong>Pros:</strong>
This is useful when the nested (or member) class is only used by the enclosing c
lass; making it a member puts it in the enclosing class scope rather than pollut
ing the outer scope with the class name. Nested classes can be forward declared
within the enclosing class and then defined in the .cpp file to avoid including
the nested class definition in the enclosing class declaration, since the nested
class definition is usually only relevant to the implementation.</p>
<p><strong>Cons:</strong>
Nested classes can be forward-declared only within the definition of the enclosi
ng class. Thus, any header file manipulating a Foo::Bar* pointer will have to in
clude the full class declaration for Foo.</p>
<p><strong>Decision:</strong>
Do not make nested classes public unless they are actually part of the interface
, e.g., a class that holds a set of options for some method.</p>
<h2><a id="user-content-nonmember-static-member-and-global-functions" class="anc
hor" href="#nonmember-static-member-and-global-functions" aria-hidden="true"><sp
an class="octicon octicon-link"></span></a>Nonmember, Static Member, and Global
Functions</h2>
<p>Prefer nonmember functions within a namespace or static member functions to g
lobal functions; use completely global functions rarely.</p>
<p><strong>Pros:</strong>
Nonmember and static member functions can be useful in some situations. Putting
nonmember functions in a namespace avoids polluting the global namespace.</p>
<p><strong>Cons:</strong>
Nonmember and static member functions may make more sense as members of a new cl
ass, especially if they access external resources or have significant dependenci
es.</p>
<p><strong>Decision:</strong></p>
<p>Sometimes it is useful, or even necessary, to define a function not bound to
a class instance. Such a function can be either a static member or a nonmember f
unction. Nonmember functions should not depend on external variables, and should
nearly always exist in a namespace. Rather than creating classes only to group
static member functions which do not share static data, use namespaces instead.<
/p>
<p>Functions defined in the same compilation unit as production classes may intr
oduce unnecessary coupling and link-time dependencies when directly called from
other compilation units; static member functions are particularly susceptible to
loop:
Foo f; <span class="pl-c">// My ctor and dtor get called once each.</span>
<span class="pl-k">for</span> (<span class="pl-k">int</span> i = <span class="pl
-c1">0</span>; i < <span class="pl-c1">1000000</span>; ++i) {
f.<span class="pl-c1">doSomething</span>(i);
}</pre></div>
<h2><a id="user-content-static-and-global-variables" class="anchor" href="#stati
c-and-global-variables" aria-hidden="true"><span class="octicon octicon-link"></
span></a>Static and Global Variables</h2>
<p>Static or global variables of class type are forbidden: they cause hard-to-fi
nd bugs due to indeterminate order of construction and destruction. However, suc
h variables are allowed if they are <code>constexpr</code>: they have no dynamic
initialization or destruction. </p>
<p>Objects with static storage duration, including global variables, static vari
ables, static class member variables, and function static variables, must be Pla
in Old Data (POD): only ints, chars, floats, or pointers, or arrays/structs of P
OD.</p>
<p>The order in which class constructors and initializers for static variables a
re called is only partially specified in C++ and can even change from build to b
uild, which can cause bugs that are difficult to find. Therefore in addition to
banning globals of class type, we do not allow static POD variables to be initia
lized with the result of a function, unless that function (such as getenv(), or
getpid()) does not itself depend on any other globals.</p>
<p>Likewise, global and static variables are destroyed when the program terminat
es, regardless of whether the termination is by returning from <code>main()</cod
e> or by calling <code>exit()</code>. The order in which destructors are called
is defined to be the reverse of the order in which the constructors were called.
Since constructor order is indeterminate, so is destructor order. For example,
at program-end time a static variable might have been destroyed, but code still
running
perhaps in another thread
tries to access it and fails. Or the destructor fo
r a static <code>string</code> variable might be run prior to the destructor for
another variable that contains a reference to that string. </p>
<p>One way to alleviate the destructor problem is to terminate the program by ca
lling <code>quick_exit()</code> instead of <code>exit()</code>. The difference i
s that <code>quick_exit()</code> does not invoke destructors and does not invoke
any handlers that were registered by calling <code>atexit()</code>. If you have
a handler that needs to run when a program terminates via <code>quick_exit()</c
ode> (flushing logs, for example), you can register it <code>using at_quick_exit
()</code>. (If you have a handler that needs to run at both exit() and quick_exi
t(), you need to register it in both places.) </p>
<p>As a result we only allow static variables to contain POD data. This rule com
pletely disallows vector (use C arrays instead), or string (use const char []).<
/p>
<p>If you need a static or global variable of a class type, consider initializin
g a pointer (which will never be freed), from either your main() function or fro
m pthread_once(). Note that this must be a raw pointer, not a "smart" pointer, s
ince the smart pointer's destructor will have the order-of-destructor issue that
we are trying to avoid.</p>
<h1><a id="user-content-classes" class="anchor" href="#classes" aria-hidden="tru
e"><span class="octicon octicon-link"></span></a>Classes</h1>
<p>Classes are the fundamental unit of code in C++. Naturally, we use them exten
sively. This section lists the main dos and don'ts you should follow when writin
g a class.</p>
<h2><a id="user-content-doing-work-in-constructors" class="anchor" href="#doingwork-in-constructors" aria-hidden="true"><span class="octicon octicon-link"></sp
an></a>Doing Work in Constructors</h2>
<p>Avoid doing complex initialization in constructors (in particular, initializa
tion that can fail or that requires virtual method calls).</p>
<p><strong>Definition:</strong>
It is possible to perform initialization in the body of the constructor.</p>
<p><strong>Pros:</strong>
Convenience in typing. No need to worry about whether the class has been initial
ized or not.</p>
<p><strong>Cons:</strong>
The problems with doing work in constructors are:</p>
<ul>
<li>There is no easy way for constructors to signal errors, short of using excep
tions (which are forbidden).</li>
<li>If the work fails, we now have an object whose initialization code failed, s
o it may be an indeterminate state.</li>
<li>If the work calls virtual functions, these calls will not get dispatched to
the subclass implementations. Future modification to your class can quietly intr
oduce this problem even if your class is not currently subclassed, causing much
confusion.</li>
<li>If someone creates a global variable of this type (which is against the rule
s, but still), the constructor code will be called before main(), possibly break
ing some implicit assumptions in the constructor code. For instance, gflags will
not yet have been initialized.</li>
</ul>
<p><strong>Decision:</strong> Constructors should never call virtual functions o
r attempt to raise non-fatal failures. If your object requires non-trivial initi
alization, consider using a factory function or Init() method.</p>
<h2><a id="user-content-initialization" class="anchor" href="#initialization" ar
ia-hidden="true"><span class="octicon octicon-link"></span></a>Initialization</h
2>
<p>If your class defines member variables, you must provide an in-class initiali
zer for every member variable or write a constructor (which can be a default con
structor). If you do not declare any constructors yourself then the compiler wil
l generate a default constructor for you, which may leave some fields uninitiali
zed or initialized to inappropriate values.</p>
<p><strong>Definition:</strong> The default constructor is called when we new a
class object with no arguments. It is always called when calling <code>new[]</co
de> (for arrays). In-class member initialization means declaring a member variab
le using a construction like <code>int _count = 17;</code> or <code>string name_
{"abc"};</code>, as opposed to just <code>int _count;</code> or <code>string _na
me;</code>.</p>
<p><strong>Pros:</strong></p>
<p><strong>Pros:</strong>
Avoids undesirable conversions.</p>
<p><strong>Cons:</strong>
None.</p>
<p><strong>Decision:</strong>
We require all single argument constructors to be explicit. Always put explicit
in front of one-argument constructors in the class definition: explicit Foo(stri
ng name);</p>
<p>The exception is copy constructors, which, in the rare cases when we allow th
em, should probably not be explicit. Classes that are intended to be transparent
wrappers around other classes are also exceptions. Such exceptions should be cl
early marked with comments.</p>
<p>Finally, constructors that take only an initializer_list may be non-explicit.
This is to permit construction of your type using the assigment form for brace
init lists (i.e. <code>MyType m = {1, 2}</code> ).</p>
<h2><a id="user-content-copy-constructors" class="anchor" href="#copy-constructo
rs" aria-hidden="true"><span class="octicon octicon-link"></span></a>Copy Constr
uctors</h2>
<p>Provide a copy constructor and assignment operator only when necessary. Other
wise, disable them with <code>DISALLOW_COPY_AND_ASSIGN</code>.</p>
<p><strong>Definition:</strong>
The copy constructor and assignment operator are used to create copies of object
s. The copy constructor is implicitly invoked by the compiler in some situations
, e.g. passing objects by value.</p>
<p><strong>Pros:</strong>
Copy constructors make it easy to copy objects. STL containers require that all
contents be copyable and assignable. Copy constructors can be more efficient tha
n CopyFrom()-style workarounds because they combine construction with copying, t
he compiler can elide them in some contexts, and they make it easier to avoid he
ap allocation.</p>
<p><strong>Cons:</strong>
Implicit copying of objects in C++ is a rich source of bugs and of performance p
roblems. It also reduces readability, as it becomes hard to track which objects
are being passed around by value as opposed to by reference, and therefore where
changes to an object are reflected.</p>
<p><strong>Decision:</strong>
Few classes need to be copyable. Most should have neither a copy constructor nor
an assignment operator. In many situations, a pointer or reference will work ju
st as well as a copied value, with better performance. For example, you can pass
function parameters by reference or pointer instead of by value, and you can st
ore pointers rather than objects in an STL container.</p>
<p>If your class needs to be copyable, prefer providing a copy method, such as <
code>clone()</code>, rather than a copy constructor, because such methods cannot
be invoked implicitly. If a copy method is insufficient in your situation (e.g.
for performance reasons, or because your class needs to be stored by value in a
n STL container), provide both a copy constructor and assignment operator.</p>
<p>If your class does not need a copy constructor or assignment operator, you mu
st explicitly disable them. To do so, add dummy declarations for the copy constr
uctor and assignment operator in the private: section of your class, but do not
provide any corresponding definition (so that any attempt to use them results in
a link error).</p>
<p>For convenience, a <code>DISALLOW_COPY_AND_ASSIGN</code> macro can be used:</
p>
<div class="highlight highlight-cpp"><pre><span class="pl-c">// A macro to disal
low the copy constructor and operator= functions</span>
<span class="pl-c">// This should be used in the private: declarations for a cla
ss</span>
#<span class="pl-k">define</span> <span class="pl-en">DISALLOW_COPY_AND_ASSIGN</
span>(<span class="pl-v">TypeName</span>) \
<span class="pl-en">TypeName</span>(<span class="pl-k">const</span> TypeName&a
mp;);
\
<span class="pl-k">void</span> <span class="pl-k">operator</span>=(<span class
="pl-k">const</span> TypeName&)</pre></div>
<p>Then, in class Foo:</p>
<div class="highlight highlight-cpp"><pre><span class="pl-k">class</span> <span
class="pl-en">Foo</span>
{
<span class="pl-k">public:</span>
<span class="pl-en">Foo</span>(<span class="pl-k">int</span> f);
<span class="pl-en">~Foo</span>();
<span class="pl-k">private:</span>
<span class="pl-en">DISALLOW_COPY_AND_ASSIGN</span>(Foo);
};</pre></div>
<h2><a id="user-content-delegating-and-inheriting-constructors" class="anchor" h
ref="#delegating-and-inheriting-constructors" aria-hidden="true"><span class="oc
ticon octicon-link"></span></a>Delegating and inheriting constructors</h2>
<p>Use delegating and inheriting constructors when they reduce code duplication.
</p>
<p><strong>Definition:</strong></p>
<p>Delegating and inheriting constructors are two different features, both intro
duced in C++11, for reducing code duplication in constructors. Delegating constr
uctors allow one of a class's constructors to forward work to one of the class's
other constructors, using a special variant of the initialization list syntax.
For example:</p>
<div class="highlight highlight-cpp"><pre><span class="pl-en">X::X</span>(<span
class="pl-k">const</span> string& name) : name_(name) {
...
}
<span class="pl-en">X::X</span>() : X(<span class="pl-s"><span class="pl-pds">"<
/span><span class="pl-pds">"</span></span>) { }</pre></div>
<p>Inheriting constructors allow a derived class to have its base class's constr
uctors available directly, just as with any of the base class's other member fun
ctions, instead of having to redeclare them. This is especially useful if the ba
se has multiple constructors. For example:</p>
<p><strong>Decision:</strong>
A class may end with <code>Interface</code> only if it meets the above requireme
nts. We do not require the converse, however: classes that meet the above requir
ements are not required to end with <code>Interface</code>.</p>
<h2><a id="user-content-operator-overloading" class="anchor" href="#operator-ove
rloading" aria-hidden="true"><span class="octicon octicon-link"></span></a>Opera
tor Overloading</h2>
<p>Do not overload operators except in rare, special circumstances.</p>
<p><strong>Definition:</strong>
A class can define that operators such as + and / operate on the class as if it
were a built-in type. An overload of <code>operator""</code> allows the built-in
literal syntax to be used to create objects of class types. </p>
<p><strong>Pros:</strong>
Operator overloading can make code appear more intuitive because a class will be
have in the same way as built-in types (such as int). Overloaded operators are m
ore playful names for functions that are less-colorfully named, such as <code>Eq
uals()</code> or <code>Add()</code>.</p>
<p>For some template functions to work correctly, you may need to define operato
rs.</p>
<p>User-defined literals are a very concise notation for creating objects of use
r-defined types.</p>
<p><strong>Cons:</strong>
While operator overloading can make code more intuitive, it has several drawback
s:</p>
<ul>
<li>It can fool our intuition into thinking that expensive operations are cheap,
built-in operations.</li>
<li>It is much harder to find the call sites for overloaded operators. Searching
for <code>equals()</code> is much easier than searching for relevant invocation
s of <code>==</code>.</li>
<li>Some operators work on pointers too, making it easy to introduce bugs. Foo +
4 may do one thing, while &Foo + 4 does something totally different. The co
mpiler does not complain for either of these, making this very hard to debug.</l
i>
<li>User-defined literals allow creating new syntactic forms that are unfamiliar
even to experienced C++ programmers. </li>
</ul>
<p>Overloading also has surprising ramifications. For instance, if a class overl
oads unary operator&, it cannot safely be forward-declared.</p>
<p><strong>Decision:</strong>
In general, do not overload operators. The assignment operator (<code>operator=<
/code>), in particular, is insidious and should be avoided. You can define funct
ions like <code>equals()</code> and <code>clone()</code> if you need them. Likew
ise, avoid the dangerous unary operator& at all costs, if there's any possib
ility the class might be forward-declared.</p>
<p>Do not overload <code>operator""</code>, i.e. do not introduce user-defined l
iterals. </p>
<p>However, there may be rare cases where you need to overload an operator to in
teroperate with templates or "standard" C++ classes (such as <code>operator<&
lt; (ostream&, const T&)</code> for logging). These are acceptable if fu
lly justified, but you should try to avoid these whenever possible. In particula
r, do not overload <code>operator==</code> or <code>operator<</code> just so
that your class can be used as a key in an STL container; instead, you should cr
eate equality and comparison functor types when declaring the container.</p>
<p>Some of the STL algorithms do require you to overload <code>operator==</code>
, and you may do so in these cases, provided you document why.</p>
<p>See also Copy Constructors and Function Overloading.</p>
<h2><a id="user-content-access-control" class="anchor" href="#access-control" ar
ia-hidden="true"><span class="octicon octicon-link"></span></a>Access Control</h
2>
<p>Make data members private, and provide access to them through accessor functi
ons as needed (for technical reasons, we allow data members of a test fixture cl
ass to be protected when using Google Test). Typically a variable would be calle
d <code>_foo</code> and the accessor function <code>getFoo()</code> . You may al
so want a mutator function <code>setFoo()</code> . Exception: static const data
members (typically called FOO) need not be private.</p>
<p>The definitions of accessors are usually inlined in the header file.</p>
<p>See also Inheritance and Function Names.</p>
<h2><a id="user-content-declaration-order" class="anchor" href="#declaration-ord
er" aria-hidden="true"><span class="octicon octicon-link"></span></a>Declaration
Order</h2>
<p>Use the specified order of declarations within a class: public: before privat
e:, methods before data members (variables), etc.</p>
<p>Your class definition should start with its public: section, followed by its
protected: section and then its private: section. If any of these sections are e
mpty, omit them.</p>
<p>Within each section, the declarations generally should be in the following or
der:</p>
<ul>
<li>Typedefs and Enums</li>
<li>Constants (<code>static const</code> data members)</li>
<li>Creators (<code>createXXX</code> methods)</li>
<li>Constructors</li>
<li>Destructor</li>
<li>Methods, including static methods</li>
<li>overriden methods (must have the <code>override</code> keyword as suffix)</l
i>
<li>Data Members (except <code>static const</code> data members)</li>
</ul>
<p>Friend declarations should always be in the private section, and the <code>DI
SALLOW_COPY_AND_ASSIGN</code> macro invocation should be at the end of the priva
te: section. It should be the last thing in the class. See Copy Constructors.</p
>
<p>Method definitions in the corresponding .cpp file should be the same as the d
<ul>
<li>Defining a move constructor (a constructor taking an rvalue reference to the
class type) makes it possible to move a value instead of copying it. If <code>v
1</code> is a <code>vector<string></code>, for example, then <code>auto v2
(std::move(v1))</code> will probably just result in some simple pointer manipula
tion instead of copying a large amount of data. In some cases this can result in
a major performance improvement.</li>
<li>Rvalue references make it possible to write a generic function wrapper that
forwards its arguments to another function, and works whether or not its argumen
ts are temporary objects.</li>
<li>Rvalue references make it possible to implement types that are moveable but
not copyable, which can be useful for types that have no sensible definition of
copying but where you might still want to pass them as function arguments, put t
hem in containers, etc.</li>
<li><code>std::move</code> is necessary to make effective use of some standard-l
ibrary types, such as <code>std::unique_ptr</code>.</li>
</ul>
<p><strong>Cons:</strong></p>
<ul>
<li>Rvalue references are a relatively new feature (introduced as part of C++11)
, and not yet widely understood. Rules like reference collapsing, and automatic
synthesis of move constructors, are complicated.</li>
<li>Rvalue references encourage a programming style that makes heavier use of va
lue semantics. This style is unfamiliar to many developers, and its performance
characteristics can be hard to reason about.</li>
</ul>
<p><strong>Decision:</strong></p>
<p>Do not use rvalue references, and do not use the <code>std::forward</code> or
<code>std::move_if_noexcept</code> utility functions (which are essentially jus
t casts to rvalue reference types), or <code>std::move_iterator</code>. Use sing
le-argument <code>std::move</code> only with objects that are not copyable (e.g.
<code>std::unique_ptr</code>), or in templated code with objects that might not
be copyable.</p>
<h2><a id="user-content-function-overloading" class="anchor" href="#function-ove
rloading" aria-hidden="true"><span class="octicon octicon-link"></span></a>Funct
ion Overloading</h2>
<p>Use overloaded functions (including constructors) only if a reader looking at
a call site can get a good idea of what is happening without having to first fi
gure out exactly which overload is being called.</p>
<p><strong>Definition:</strong></p>
<p>You may write a function that takes a <code>const string&</code> and over
load it with another that takes <code>const char*</code> .</p>
<div class="highlight highlight-cpp"><pre><span class="pl-k">class</span> <span
class="pl-en">MyClass</span>
{
<span class="pl-k">public:</span>
<span class="pl-k">void</span> <span class="pl-en">analyze</span>(<span clas
s="pl-k">const</span> string &text);
<span class="pl-k">void</span> <span class="pl-en">analyze</span>(<span clas
s="pl-k">const</span> <span class="pl-k">char</span> *text, <span class="pl-c1">
size_t</span> textlen);
};</pre></div>
<p><strong>Pros:</strong> Overloading can make code more intuitive by allowing a
n identically-named function to take different arguments. It may be necessary fo
r templatized code, and it can be convenient for Visitors.</p>
<p><strong>Cons:</strong> If a function is overloaded by the argument types alon
e, a reader may have to understand C++'s complex matching rules in order to tell
what's going on. Also many people are confused by the semantics of inheritance
if a derived class overrides only some of the variants of a function.</p>
<p><strong>Decision:</strong> If you want to overload a function, consider quali
fying the name with some information about the arguments, e.g., <code>appendStri
ng()</code> , <code>appendInt()</code> rather than just <code>append()</code> .<
/p>
<h2><a id="user-content-default-arguments" class="anchor" href="#default-argumen
ts" aria-hidden="true"><span class="octicon octicon-link"></span></a>Default Arg
uments</h2>
<p>We do not allow default function parameters, except in limited situations as
explained below. Simulate them with function overloading instead, if appropriate
.</p>
<p><strong>Pros:</strong> Often you have a function that uses default values, bu
t occasionally you want to override the defaults. Default parameters allow an ea
sy way to do this without having to define many functions for the rare exception
s. Compared to overloading the function, default arguments have a cleaner syntax
, with less boilerplate and a clearer distinction between 'required' and 'option
al' arguments.</p>
<p><strong>Cons:</strong> Function pointers are confusing in the presence of def
ault arguments, since the function signature often doesn't match the call signat
ure. Adding a default argument to an existing function changes its type, which c
an cause problems with code taking its address. Adding function overloads avoids
these problems. In addition, default parameters may result in bulkier code sinc
e they are replicated at every call-site -- as opposed to overloaded functions,
where "the default" appears only in the function definition.</p>
<p><strong>Decision:</strong></p>
<p>While the cons above are not that onerous, they still outweigh the (small) be
nefits of default arguments over function overloading. So except as described be
low, we require all arguments to be explicitly specified.</p>
<p>One specific exception is when the function is a static function (or in an un
named namespace) in a .cpp file. In this case, the cons don't apply since the fu
nction's use is so localized.</p>
<p>In particular, the <code>createXXX</code> and <code>initXXX</code> methods in
cocos2dx are allowed to use default arguments.</p>
<p>Another specific exception is when default arguments are used to simulate var
iable-length argument lists. Example:</p>
<div class="highlight highlight-cpp"><pre><span class="pl-c">// Support up to 4
params by using a default empty AlphaNum.</span>
string <span class="pl-en">strCat</span>(<span class="pl-k">const</span> AlphaNu
m &a,
</ul>
<p>Note that the <code>PRI*</code> macros expand to independent strings which ar
e concatenated by the compiler. Hence if you are using a non-constant format str
ing, you need to insert the value of the macro into the format, rather than the
name. It is still possible, as usual, to include length specifiers, etc., after
the % when using the PRI* macros. So, e.g. <code>printf("x = %30"PRIuS"\n", x)</
code> would expand on 32-bit Linux to <code>printf("x = %30" "u" "\n", x)</code>
, which the compiler will treat as <code>printf("x = %30u\n", x)</code>.</p>
<ul>
<li>Remember that <code>sizeof(void *) != sizeof(int)</code>. Use <code>intptr_t
</code> if you want a pointer-sized integer.</li>
<li>You may need to be careful with structure alignments, particularly for struc
tures being stored on disk. Any class/structure with a int64_t/uint64_t member w
ill by default end up being 8-byte aligned on a 64-bit system. If you have such
structures being shared on disk between 32-bit and 64-bit code, you will need to
ensure that they are packed the same on both architectures. Most compilers offe
r a way to alter structure alignment. For gcc, you can use <code>__attribute__((
packed))</code>. MSVC offers <code>#pragma pack()</code> and <code>__declspec(al
ign())</code>.</li>
<li>Use the <code>LL</code> or <code>ULL</code> suffixes as needed to create 64bit constants. For example:</li>
</ul>
<div class="highlight highlight-cpp"><pre><span class="pl-c1">int64_t</span> my_
value = 0x123456789LL;
<span class="pl-c1">uint64_t</span> my_mask = <span class="pl-c1">3ULL</span> &l
t;< <span class="pl-c1">48</span>;</pre></div>
<pre><code>If you really need different code on 32-bit and 64-bit systems, use #
ifdef _LP64 to choose between the code variants. (But please avoid this if possi
ble, and keep any such changes localized.)
</code></pre>
<h2><a id="user-content-preprocessor-macros" class="anchor" href="#preprocessormacros" aria-hidden="true"><span class="octicon octicon-link"></span></a>Preproc
essor Macros</h2>
<p>Be very cautious with macros. Prefer inline functions, enums, and const varia
bles to macros.</p>
<p>Macros mean that the code you see is not the same as the code the compiler se
es. This can introduce unexpected behavior, especially since macros have global
scope.</p>
<p>Luckily, macros are not nearly as necessary in C++ as they are in C. Instead
of using a macro to inline performance-critical code, use an inline function. In
stead of using a macro to store a constant, use a const variable. Instead of usi
ng a macro to "abbreviate" a long variable name, use a reference. Instead of usi
ng a macro to conditionally compile code ... well, don't do that at all (except,
of course, for the #define guards to prevent double inclusion of header files).
It makes testing much more difficult.</p>
<p>Macros can do things these other techniques cannot, and you do see them in th
e codebase, especially in the lower-level libraries. And some of their special f
eatures (like stringifying, concatenation, and so forth) are not available throu
gh the language proper. But before using a macro, consider carefully whether the
re's a non-macro way to achieve the same result.</p>
<p>The following usage pattern will avoid many problems with macros; if you use
macros, follow it whenever possible:</p>
<ul>
<li>Don't define macros in a .h file.</li>
<li><code>#define</code> macros right before you use them, and <code>#undef</cod
e> them right after.</li>
<li>Do not just #undef an existing macro before replacing it with your own; inst
ead, pick a name that's likely to be unique.</li>
<li>Try not to use macros that expand to unbalanced C++ constructs, or at least
document that behavior well.</li>
<li>Prefer not using ## to generate function/class/variable names.</li>
</ul>
<h2><a id="user-content-0-and-nullptrnull" class="anchor" href="#0-and-nullptrnu
ll" aria-hidden="true"><span class="octicon octicon-link"></span></a>0 and nullp
tr/NULL</h2>
<p>Use 0 for integers, 0.0 for reals, nullptr (or NULL) for pointers, and '\0' f
or chars.</p>
<p>Use 0 for integers and 0.0 for reals. This is not controversial.</p>
<p>For pointers (address values), there is a choice between <code>0</code>, <cod
e>NULL</code>, and <code>nullptr</code>. For projects that allow C++11 features,
use <code>nullptr</code>. For C++03 projects, we prefer <code>NULL</code> becau
se it looks like a pointer. In fact, some C++ compilers provide special definiti
ons of <code>NULL</code> which enable them to give useful warnings, particularly
in situations where <code>sizeof(NULL)</code> is not equal to <code>sizeof(0)</
code>.</p>
<p>Use <code>'\0'</code> for chars. This is the correct type and also makes code
more readable.</p>
<h2><a id="user-content-sizeof" class="anchor" href="#sizeof" aria-hidden="true"
><span class="octicon octicon-link"></span></a>sizeof</h2>
<p>Prefer <code>sizeof(varname)</code> to <code>sizeof(type)</code>.</p>
<p>Use <code>sizeof(varname)</code> when you take the size of a particular varia
ble. <code>sizeof(varname)</code> will update appropriately if someone changes t
he variable type either now or later. You may use <code>sizeof(type)</code> for
code unrelated to any particular variable, such as code that manages an external
or internal data format where a variable of an appropriate C++ type is not conv
enient.</p>
<div class="highlight highlight-cpp"><pre>Struct data;
<span class="pl-en">memset</span>(&data, <span class="pl-c1">0</span>, <span
class="pl-k">sizeof</span>(data)); <span class="pl-c">// GOOD</span>
<span class="pl-en">memset</span>(&data, <span class="pl-c1">0</span>, <span
class="pl-k">sizeof</span>(Struct)); <span class="pl-c">// BAD</span>
<span class="pl-k">if</span> (raw_size < <span class="pl-k">sizeof</span>(<sp
an class="pl-k">int</span>)) {
<span class="pl-c1">LOG</span>(ERROR) << <span class="pl-s"><span clas
s="pl-pds">"</span>compressed record not big enough for count: <span class="pl-p
ds">"</span></span> << raw_size;
<span class="pl-k">return</span> <span class="pl-c1">false</span>;
}</pre></div>
code>const auto&</code> or they'll get copies when they didn't mean to.</p>
<p>The interaction between auto and C++11 brace-initialization can be confusing.
The declarations</p>
<div class="highlight highlight-cpp"><pre><span class="pl-k">auto</span> <span c
lass="pl-en">x</span>(<span class="pl-c1">3</span>); <span class="pl-c">// Note
: parentheses.</span>
<span class="pl-k">auto</span> y{<span class="pl-c1">3</span>}; <span class="pl
-c">// Note: curly braces.</span></pre></div>
<p>mean different things
x is an int, while y is an initializer_list. The same app
lies to other normally-invisible proxy types.</p>
<p>If an <code>auto</code> variable is used as part of an interface, e.g. as a c
onstant in a header, then a programmer might change its type while only intendin
g to change its value, leading to a more radical API change than intended.</p>
<p><strong>Decision:</strong></p>
<p><code>auto</code> is permitted, for local variables only. Do not use <code>au
to</code> for file-scope or namespace-scope variables, or for class members. Nev
er assign a braced initializer list to an auto-typed variable.</p>
<p>The <code>auto</code> keyword is also used in an unrelated C++11 feature: it'
s part of the syntax for a new kind of function declaration with a trailing retu
rn type. Function declarations with trailing return types are not permitted.</p>
<h2><a id="user-content-brace-initialization" class="anchor" href="#brace-initia
lization" aria-hidden="true"><span class="octicon octicon-link"></span></a>Brace
Initialization</h2>
<p>You may use brace initialization.</p>
<p>In C++03, aggregate types (arrays and structs with no constructor) could be i
nitialized using braces.</p>
<div class="highlight highlight-cpp"><pre><span class="pl-k">struct</span> <span
class="pl-en">Point</span> { <span class="pl-k">int</span> x; <span class="pl-k
">int</span> y; };
<span class="pl-c1">Point</span> p = {<span class="pl-c1">1</span>, <span class=
"pl-c1">2</span>};</pre></div>
<p>In C++11, this syntax has been expanded for use with all other datatypes. The
brace initialization form is called braced-init-list. Here are a few examples o
f its use.</p>
<div class="highlight highlight-cpp"><pre><span class="pl-c">// Vector takes lis
ts of elements.</span>
vector<string> v{<span class="pl-s"><span class="pl-pds">"</span>foo<span
class="pl-pds">"</span></span>, <span class="pl-s"><span class="pl-pds">"</span>
bar<span class="pl-pds">"</span></span>};
<span class="pl-c">// The same, except this form cannot be used if the initializ
er_list</span>
<span class="pl-c">// constructor is explicit. You may choose to use either form
.</span>
vector<string> v = {<span class="pl-s"><span class="pl-pds">"</span>foo<sp
an class="pl-pds">"</span></span>, <span class="pl-s"><span class="pl-pds">"</sp
an>bar<span class="pl-pds">"</span></span>};
);
};
MyOtherType m = {<span class="pl-c1">1</span>, <span class="pl-s"><span class="p
l-pds">"</span>b<span class="pl-pds">"</span></span>};
<span class="pl-c">// If the constructor is explicit, you can't use the "= {}" f
orm.</span>
MyOtherType m{<span class="pl-s"><span class="pl-pds">"</span>b<span class="pl-p
ds">"</span></span>};</pre></div>
<p>Never assign a braced-init-list to an auto local variable. In the single elem
ent case, what this means can be confusing.</p>
<div class="highlight highlight-cpp"><pre><span class="pl-k">auto</span> d = {<s
pan class="pl-c1">1.23</span>};
<span class="pl-c">// d is an initializer
_list<double></span>
<span class="pl-k">auto</span> d = <span class="pl-k">double</span>{<span class=
"pl-c1">1.23</span>}; <span class="pl-c">// Good -- d is a double, not an initi
alizer_list.</span></pre></div>
<h2><a id="user-content-lambda-expressions" class="anchor" href="#lambda-express
ions" aria-hidden="true"><span class="octicon octicon-link"></span></a>Lambda ex
pressions</h2>
<p>Use lambda expressions, or the related <code>std::function</code> or <code>st
d::bind</code> utilities only in special places like cocos2d callbacks.</p>
<p><strong>Definition:</strong> Lambda expressions are a concise way of creating
anonymous function objects. They're often useful when passing functions as argu
ments. For example: <code>std::sort(v.begin(), v.end(), [](string x, string y) {
return x[1] < y[1]; });</code> Lambdas were introduced in C++11 along with a
set of utilities for working with function objects, such as the polymorphic wra
pper <code>std::function</code>.</p>
<p><strong>Pros:</strong></p>
<ul>
<li>Lambdas are much more concise than other ways of defining function objects t
o be passed to STL algorithms, which can be a readability improvement.</li>
<li>Lambdas, <code>std::function</code>, and <code>std::bind</code> can be used
in combination as a general purpose callback mechanism; they make it easy to wri
te functions that take bound functions as arguments.</li>
</ul>
<p><strong>Cons:</strong></p>
<ul>
<li>Variable capture in lambdas can be tricky, and might be a new source of dang
ling-pointer bugs.</li>
<li>It's possible for use of lambdas to get out of hand; very long nested anonym
ous functions can make code harder to understand.</li>
</ul>
<p>Decision:</p>
<p>Use lambda expressions, or the related <code>std::function</code> or <code>st
d::bind</code> utilities only in special places like cocos2d callbacks.</p>
<h2><a id="user-content-boost" class="anchor" href="#boost" aria-hidden="true"><
span class="octicon octicon-link"></span></a>Boost</h2>
Meani
Ambig
Ambig
Only
Lots
Delet
CCTextureCache.cpp
CCTexture2D.cpp
</code></pre>
<p>C++ files should end in <code>.cpp</code> and header files should end in <cod
e>.h</code>.</p>
<p>Do not use filenames that already exist in /usr/include, such as db.h.</p>
<p>In general, make your filenames very specific. For example, use <code>CCTextu
re2D.h</code> rather than <code>Texture.h</code>. A very common case is to have
a pair of files called, e.g., <code>FooBar.h</code> and <code>FooBar.cpp</code>
, defining a class called <code>FooBar</code> .</p>
<p>Inline functions must be in a <code>.h</code> file. If your inline functions
are very short, they should go directly into your .h file. However, if your inli
ne functions include a lot of code, they may go into a third file that ends in <
code>-inl.h</code> . In a class with a lot of inline code, your class could have
three files:</p>
<div class="highlight highlight-cpp"><pre>UrlTable.h
<span class="pl-c">//
The class declaration.</span>
UrlTable.cpp
<span class="pl-c">// The class definition.</span>
UrlTable-inl.h <span class="pl-c">// Inline functions that include lots of cod
e.</span></pre></div>
<p>See also the section -inl.h Files</p>
<h2><a id="user-content-type-names" class="anchor" href="#type-names" aria-hidde
n="true"><span class="octicon octicon-link"></span></a>Type Names</h2>
<p>Type names start with a capital letter and have a capital letter for each new
word, with no underscores: <code>MyExcitingClass</code>, <code>MyExcitingEnum</
code>.</p>
<p>The names of all types
classes, structs, typedefs, and enums
have the same naming
convention. Type names should start with a capital letter and have a capital le
tter for each new word. No underscores. For example:</p>
<div class="highlight highlight-cpp"><pre><span class="pl-c">// classes and stru
cts</span>
<span class="pl-k">class</span> <span class="pl-en">UrlTable</span> { ...
<span class="pl-k">class</span> <span class="pl-en">UrlTableTester</span> { ...
<span class="pl-k">struct</span> <span class="pl-en">UrlTableProperties</span> {
...
<span class="pl-c">// typedefs</span>
<span class="pl-k">typedef</span> hash_map<UrlTableProperties *, string> P
ropertiesMap;
<span class="pl-c">// enums</span>
<span class="pl-k">enum</span> UrlTableErrors { ...</pre></div>
<h2><a id="user-content-variable-names" class="anchor" href="#variable-names" ar
ia-hidden="true"><span class="octicon octicon-link"></span></a>Variable Names</h
2>
<p>Variable names are all camelCase (they start with lowercase). Class member va
riables have an underscore as prefix. For instance: <code>myExcitingLocalVariabl
e</code> , <code>_myExcitingLocalVariable</code> .
<p>If you are naming something that is analogous to an existing C or C++ entity
then you can follow the existing naming convention scheme.</p>
<h3><a id="user-content-bigopen" class="anchor" href="#bigopen" aria-hidden="tru
e"><span class="octicon octicon-link"></span></a><code>bigopen()</code></h3>
<p>function name, follows form of open() </p>
<h3><a id="user-content-uint" class="anchor" href="#uint" aria-hidden="true"><sp
an class="octicon octicon-link"></span></a><code>uint</code></h3>
<p>typedef </p>
<h3><a id="user-content-bigpos" class="anchor" href="#bigpos" aria-hidden="true"
><span class="octicon octicon-link"></span></a><code>bigpos</code></h3>
<p>struct or class, follows form of pos </p>
<h3><a id="user-content-sparse_hash_map" class="anchor" href="#sparse_hash_map"
aria-hidden="true"><span class="octicon octicon-link"></span></a><code>sparse_ha
sh_map</code></h3>
<p>STL-like entity; follows STL naming conventions </p>
<h3><a id="user-content-longlong_max" class="anchor" href="#longlong_max" aria-h
idden="true"><span class="octicon octicon-link"></span></a><code>LONGLONG_MAX</c
ode></h3>
<p>a constant, as in INT_MAX </p>
<h1><a id="user-content-comments" class="anchor" href="#comments" aria-hidden="t
rue"><span class="octicon octicon-link"></span></a>Comments</h1>
<p>Though a pain to write, comments are absolutely vital to keeping our code rea
dable. The following rules describe what you should comment and where. But remem
ber: while comments are very important, the best code is self-documenting. Givin
g sensible names to types and variables is much better than using obscure names
that you must then explain through comments.</p>
<p>When writing your comments, write for your audience: the next contributor who
will need to understand your code. Be generous
the next one may be you!</p>
<h2><a id="user-content-doxygen" class="anchor" href="#doxygen" aria-hidden="tru
e"><span class="octicon octicon-link"></span></a>Doxygen</h2>
<ul>
<li>Use Doxygen strings in the header files. Doxygen comments on the implementat
ion file are not required.</li>
<li>All public <code>class</code> objects MUST have a Doxygen comment that expla
ins what it does.</li>
<li>All <code>public</code> methods of the class, with the exception of overridd
en methods, MUST be documented using Doxygen comments.</li>
<li><code>protected</code> and <code>private</code> methods are suggested to hav
e Doxygen comments, but are not mandatory.</li>
<li>Instance variables should NOT have Doxygen comments, unless they are public.
</li>
<li>Markdown systax is allowed within your Doxygen comments</li>
</ul>
<p>Example:</p>
<div class="highlight highlight-cpp"><pre><span class="pl-c">/** `WorldPeace` ex
tends `Node` by adding enough power to create world peace.</span>
<span class="pl-c"> *</span>
<span class="pl-c"> * `WorldPeace` should be used only when the world is about t
o collapse.</span>
<span class="pl-c"> * Do not create an instance of `WorldPeace` if the `Scene` h
as a peace level of 5.</span>
<span class="pl-c"> * </span>
<span class="pl-c"> */</span>
<span class="pl-k">class</span> <span class="pl-en">WorldPeace</span> : <span cl
ass="pl-k">public</span> <span class="pl-en">Node</span>
{
<span class="pl-k">public:</span>
<span class="pl-c">/** creates a `WorldPeace` with a predefined number of pr
eachers</span>
<span class="pl-c">
*/</span>
<span class="pl-k">static</span> WorldPeace* <span class="pl-en">create</spa
n>(<span class="pl-k">int</span> numberOfPreachers);
<span class="pl-c">/** sets the number of preachers that will try to create
the world peace.</span>
<span class="pl-c">
The more the better. But be aware that corruption might a
ppear if the number if higher than the 20% of the population.</span>
<span class="pl-c">
*/</span>
<span class="pl-k">void</span> <span class="pl-en">setNumberOfPreachers</spa
n>(<span class="pl-k">int</span> numberOfPreachers);
<span class="pl-c">/** displays an aura around the WorldPeace object </span>
<span class="pl-c">
@see See all `displaySuperAura()`</span>
<span class="pl-c">
*/</span>
<span class="pl-k">void</span> <span class="pl-en">displayAura</span>();
<span class="pl-c">// Overrides</span>
<span class="pl-k">virtual</span> <span class="pl-k">void</span> <span class
="pl-en">addChild</span>(Node * child) <span class="pl-k">override</span>;
<span class="pl-k">virtual</span> <span class="pl-k">void</span> <span class
="pl-en">removeChild</span>(Node* child, <span class="pl-k">bool</span> cleanup)
<span class="pl-k">override</span>;
<span class="pl-k">protected:</span>
<span class="pl-en">WorldPeace</span>();
<span class="pl-k">virtual</span> <span class="pl-en">~WorldPeace</span>();
<span class="pl-k">bool</span> <span class="pl-en">init</span>(<span class="
pl-k">int</span> nubmerOfPreachers);
<span class="pl-k">int</span> _nubmerOfPreachers;
};</pre></div>
<h2><a id="user-content-comment-style" class="anchor" href="#comment-style" aria
-hidden="true"><span class="octicon octicon-link"></span></a>Comment Style</h2>
<p>Use either the <code>//</code> or <code>/* */</code> syntax, as long as you a
re consistent.</p>
<p>You can use either the <code>//</code> or the <code>/* */</code> syntax; howe
ver, <code>//</code> is much more common. Be consistent with how you comment and
what style you use where.</p>
<span class="pl-c">// that we don't yet know how many entries the table has.</s
pan>
<span class="pl-k">int</span> _numTotalEntries;</pre></div>
<h3><a id="user-content-global-variables-1" class="anchor" href="#global-variabl
es-1" aria-hidden="true"><span class="octicon octicon-link"></span></a>Global Va
riables</h3>
<p>As with data members, all global variables should have a comment describing w
hat they are and what they are used for. For example:</p>
<div class="highlight highlight-cpp"><pre><span class="pl-c">// The total number
of tests cases that we run through in this regression test.</span>
<span class="pl-k">const</span> <span class="pl-k">int</span> NUM_TEST_CASES = <
span class="pl-c1">6</span>;</pre></div>
<h2><a id="user-content-implementation-comments" class="anchor" href="#implement
ation-comments" aria-hidden="true"><span class="octicon octicon-link"></span></a
>Implementation Comments</h2>
<p>In your implementation you should have comments in tricky, non-obvious, inter
esting, or important parts of your code.</p>
<h3><a id="user-content-class-data-members-2" class="anchor" href="#class-data-m
embers-2" aria-hidden="true"><span class="octicon octicon-link"></span></a>Class
Data Members</h3>
<p>Tricky or complicated code blocks should have comments before them. Example:<
/p>
<div class="highlight highlight-cpp"><pre><span class="pl-c">// Divide result by
two, taking into account that x</span>
<span class="pl-c">// contains the carry from the add.</span>
<span class="pl-k">for</span> (<span class="pl-k">int</span> i = <span class="pl
-c1">0</span>; i < result-><span class="pl-en">size</span>(); i++) {
x = (x << <span class="pl-c1">8</span>) + (*result)[i];
(*result)[i] = x >> <span class="pl-c1">1</span>;
x &= <span class="pl-c1">1</span>;
}</pre></div>
<h3><a id="user-content-line-comments" class="anchor" href="#line-comments" aria
-hidden="true"><span class="octicon octicon-link"></span></a>Line Comments</h3>
<p>Also, lines that are non-obvious should get a comment at the end of the line.
These end-of-line comments should be separated from the code by 2 spaces. Examp
le:</p>
<div class="highlight highlight-cpp"><pre><span class="pl-c">// If we have enoug
h memory, mmap the data portion too.</span>
mmap_budget = max<int64>(<span class="pl-c1">0</span>, mmap_budget - index
_-><span class="pl-en">length</span>());
<span class="pl-k">if</span> (mmap_budget >= data_size_ && !MmapData(
mmap_chunk_bytes, mlock))
<span class="pl-k">return</span>; <span class="pl-c">// Error already logge
d.</span></pre></div>
<p>Note that there are both comments that describe what the code is doing, and c
omments that mention that an error has already been logged when the function ret
urns.</p>
<p>If you have several comments on subsequent lines, it can often be more readab
le to line them up:</p>
<div class="highlight highlight-cpp"><pre><span class="pl-en">doSomething</span>
();
<span class="pl-c">// Comment here so the comments line up.
</span>
<span class="pl-en">doSomethingElseThatIsLonger</span>(); <span class="pl-c">//
Comment here so there are two spaces between</span>
<span class="pl-c">// the code and the comment.<
/span>
{ <span class="pl-c">// One space before comment when opening a new scope is all
owed,</span>
<span class="pl-c">// thus the comment lines up with the following comments an
d code.</span>
<span class="pl-c1">doSomethingElse</span>(); <span class="pl-c">// Two spa
ces before line comments normally.</span>
}
<span class="pl-en">doSomething</span>(); <span class="pl-c">/* For trailing blo
ck comments, one space is fine. */</span></pre></div>
<h3><a id="user-content-nullptrnull-truefalse-1-2-3" class="anchor" href="#nullp
trnull-truefalse-1-2-3" aria-hidden="true"><span class="octicon octicon-link"></
span></a>nullptr/NULL, true/false, 1, 2, 3...</h3>
<p>When you pass in a null pointer, boolean, or literal integer values to functi
ons, you should consider adding a comment about what they are, or make your code
self-documenting by using constants. For example, compare:</p>
<div class="highlight highlight-cpp"><pre><span class="pl-k">bool</span> success
= calculateSomething(interesting_value,
<span class="pl-c1">10</span>,
<span class="pl-c1">false</span>,
<span class="pl-c1">NULL</span>); <span class
="pl-c">// What are these arguments??</span></pre></div>
<p>versus:</p>
<div class="highlight highlight-cpp"><pre><span class="pl-k">bool</span> success
= calculateSomething(interesting_value,
<span class="pl-c1">10</span>,
<span class
="pl-c">// Default base value.</span>
<span class="pl-c1">false</span>, <span class
="pl-c">// Not the first time we're calling this.</span>
<span class="pl-c1">NULL</span>); <span class
="pl-c">// No callback.</span></pre></div>
<p>Or alternatively, constants or self-describing variables:</p>
<div class="highlight highlight-cpp"><pre><span class="pl-k">const</span> <span
class="pl-k">int</span> DEFAULT_BASE_VALUE = <span class="pl-c1">10</span>;
<span class="pl-k">const</span> <span class="pl-k">bool</span> FIRST_TIME_CALLIN
G = <span class="pl-c1">false</span>;
Callback *nullCallback = <span class="pl-c1">NULL</span>;
<span class="pl-k">bool</span> success = CalculateSomething(interestingValue,
DEFAULT_BASE_VALUE,
FIRST_TIME_CALLING,
nullCallback);</pre></div>
<h3><a id="user-content-donts" class="anchor" href="#donts" aria-hidden="true"><
span class="octicon octicon-link"></span></a>Don'ts</h3>
<p>Note that you should never describe the code itself. Assume that the person r
eading the code knows C++ better than you do, even though he or she does not kno
w what you are trying to do:</p>
<div class="highlight highlight-cpp"><pre><span class="pl-c">// Now go through t
he b array and make sure that if i occurs,</span>
<span class="pl-c">// the next element is i+1.</span>
...
<span class="pl-c">// Geez. What a useless comment.</span></pre></di
v>
<h2><a id="user-content-punctuation-spelling-and-grammar" class="anchor" href="#
punctuation-spelling-and-grammar" aria-hidden="true"><span class="octicon octico
n-link"></span></a>Punctuation, Spelling and Grammar</h2>
<p>Pay attention to punctuation, spelling, and grammar; it is easier to read wel
l-written comments than badly written ones.</p>
<p>Comments should be as readable as narrative text, with proper capitalization
and punctuation. In many cases, complete sentences are more readable than senten
ce fragments. Shorter comments, such as comments at the end of a line of code, c
an sometimes be less formal, but you should be consistent with your style.</p>
<p>Although it can be frustrating to have a code reviewer point out that you are
using a comma when you should be using a semicolon, it is very important that s
ource code maintain a high level of clarity and readability. Proper punctuation,
spelling, and grammar help with that goal.</p>
<h2><a id="user-content-todo-comments" class="anchor" href="#todo-comments" aria
-hidden="true"><span class="octicon octicon-link"></span></a>TODO Comments</h2>
<p>Use TODO comments for code that is temporary, a short-term solution, or goodenough but not perfect.</p>
<p>TODOs should include the string TODO in all caps, followed by the name, e-mai
l address, or other identifier of the person who can best provide context about
the problem referenced by the TODO. A colon is optional. The main purpose is to
have a consistent TODO format that can be searched to find the person who can pr
ovide more details upon request. A TODO is not a commitment that the person refe
renced will fix the problem. Thus when you create a TODO, it is almost always yo
ur name that is given.</p>
<div class="highlight highlight-cpp"><pre><span class="pl-c">// TODO(kl`gmail.co
m): Use a "*" here for concatenation operator.</span>
<span class="pl-c">// TODO(Zeke) change this to use relations.</span></pre></div
>
<p>If your TODO is of the form "At a future date do something" make sure that yo
u either include a very specific date ("Fix by November 2005") or a very specifi
c event ("Remove this code when all clients can handle XML responses.").</p>
<h2><a id="user-content-deprecation-comments" class="anchor" href="#deprecationcomments" aria-hidden="true"><span class="octicon octicon-link"></span></a>Depre
cation Comments</h2>
<p>Use the <code>CC_DEPRECATED_ATTRIBUTE</code> macro to mark an methods as depr
ecated.</p>
<p>Also use the <code></code><code>deprecated</code> doxygen docstring to mark i
t as deprecated in the documentation.</p>
<p>A deprecation comment must include simple, clear directions for people to fix
their callsites. In C++, you can implement a deprecated function as an inline f
unction that calls the new interface point.</p>
<p>Marking an interface point DEPRECATED will not magically cause any callsites
to change. If you want people to actually stop using the deprecated facility, yo
u will have to fix the callsites yourself or recruit a crew to help you.</p>
<p>New code should not contain calls to deprecated interface points. Use the new
interface point instead. If you cannot understand the directions, find the pers
on who created the deprecation and ask them for help using the new interface poi
nt.</p>
<h1><a id="user-content-formatting" class="anchor" href="#formatting" aria-hidde
n="true"><span class="octicon octicon-link"></span></a>Formatting</h1>
<p>Coding style and formatting are pretty arbitrary, but a project is much easie
r to follow if everyone uses the same style. Individuals may not agree with ever
y aspect of the formatting rules, and some of the rules may take some getting us
ed to, but it is important that all project contributors follow the style rules
so that they can all read and understand everyone's code easily.</p>
<p>To help you format code correctly, we've created a settings file for emacs.</
p>
<h2><a id="user-content-line-length" class="anchor" href="#line-length" aria-hid
den="true"><span class="octicon octicon-link"></span></a>Line Length</h2>
<p>Each line of text in your code should be at most 80 characters long.</p>
<p>We recognize that this rule is controversial, but so much existing code alrea
dy adheres to it, and we feel that consistency is important.</p>
<p><strong>Pros:</strong> Those who favor this rule argue that it is rude to for
ce them to resize their windows and there is no need for anything longer. Some f
olks are used to having several code windows side-by-side, and thus don't have r
oom to widen their windows in any case. People set up their work environment ass
uming a particular maximum window width, and 80 columns has been the traditional
standard. Why change it?</p>
<p><strong>Cons:</strong> Proponents of change argue that a wider line can make
code more readable. The 80-column limit is an hidebound throwback to 1960s mainf
rames; modern equipment has wide screens that can easily show longer lines.</p>
<p><strong>Decision:</strong></p>
<p>80 characters is the maximum.</p>
<p>Exception: if a comment line contains an example command or a literal URL lon
ger than 80 characters, that line may be longer than 80 characters for ease of c
ut and paste.</p>
<p>Exception: an #include statement with a long path may exceed 80 columns. Try
to avoid situations where this becomes necessary.</p>
<p>Exception: you needn't be concerned about header guards that exceed the maxim
um length.</p>
<h2><a id="user-content-non-ascii-characters" class="anchor" href="#non-ascii-ch
{
<span class="pl-c1">DoSomething</span>();
...
}</pre></div>
<p>or if you cannot fit even the first parameter:</p>
<div class="highlight highlight-cpp"><pre>ReturnType <span class="pl-en">LongCla
ssName::ReallyReallyReallyLongFunctionName</span>(
Type par_name1, <span class="pl-c">// 4 space indent</span>
Type par_name2,
Type par_name3)
{
<span class="pl-c1">DoSomething</span>(); <span class="pl-c">// 4 space ind
ent</span>
...
}</pre></div>
<p>Some points to note:</p>
<ul>
<li>If you cannot fit the return type and the function name on a single line, br
eak between them.</li>
<li>If you break after the return type of a function definition, do not indent.<
/li>
<li>The open parenthesis is always on the same line as the function name.</li>
<li>There is never a space between the function name and the open parenthesis.</
li>
<li>There is never a space between the parentheses and the parameters.</li>
<li>The open curly brace is always at the end of the same line as the last param
eter.</li>
<li>The close curly brace is either on the last line by itself or (if other styl
e rules permit) on the same line as the open curly brace.</li>
<li>There should be a space between the close parenthesis and the open curly bra
ce.</li>
<li>All parameters should be named, with identical names in the declaration and
implementation.</li>
<li>All parameters should be aligned if possible.</li>
<li>Default indentation is 4 spaces.</li>
<li>Wrapped parameters have a 4 space indent.</li>
</ul>
<p>If some parameters are unused, comment out the variable name in the function
definition:</p>
<div class="highlight highlight-cpp"><pre><span class="pl-c">// Always have name
d parameters in interfaces.</span>
<span class="pl-k">class</span> <span class="pl-en">Shape</span>
{
<span class="pl-k">public:</span>
<span class="pl-k">virtual</span> <span class="pl-k">void</span> <span class
="pl-en">rotate</span>(<span class="pl-k">double</span> radians) = 0;
}<span class="pl-ii"></span>
<span class="pl-c">// Always have named parameters in the declaration.</span>
<span class="pl-k">class</span> <span class="pl-en">Circle</span> : <span class=
"pl-k">public</span> <span class="pl-en">Shape</span>
{
<span class="pl-k">public:</span>
<span class="pl-k">virtual</span> <span class="pl-k">void</span> <span class
}</pre></div>
<p>Empty loop bodies should use <code>{}</code> or <code>continue</code>, but no
t a single semicolon.</p>
<div class="highlight highlight-cpp"><pre><span class="pl-k">while</span> (condi
tion) {
<span class="pl-c">// Repeat test until it returns false.</span>
}
<span class="pl-k">for</span> (<span class="pl-k">int</span> i = <span class="pl
-c1">0</span>; i < SOME_NUMBER; ++i) {} <span class="pl-c">// Good - empty b
ody.</span>
<span class="pl-k">while</span> (condition) <span class="pl-k">continue</span>;
<span class="pl-c">// Good - continue indicates no logic.</span>
<span class="pl-k">while</span> (condition); <span class="pl-c">// Bad - looks
like part of do/while loop.</span></pre></div>
<h2><a id="user-content-pointer-and-reference-expressions" class="anchor" href="
#pointer-and-reference-expressions" aria-hidden="true"><span class="octicon octi
con-link"></span></a>Pointer and Reference Expressions</h2>
<p>No spaces around period or arrow. Pointer operators do not have trailing spac
es.</p>
<p>The following are examples of correctly-formatted pointer and reference expre
ssions:</p>
<div class="highlight highlight-cpp"><pre>x = *p;
p = &x;
x = r.y;
x = r->y;</pre></div>
<p>Note that:</p>
<ul>
<li>There are no spaces around the period or arrow when accessing a member.</li>
<li>Pointer operators have no space after the * or &.</li>
</ul>
<p>When declaring a pointer variable or argument, you may place the asterisk adj
acent to either the type or to the variable name:</p>
<div class="highlight highlight-cpp"><pre><span class="pl-c">// These are fine,
space preceding.</span>
<span class="pl-k">char</span> *c;
<span class="pl-k">const</span> string &str;
<span
<span
char*
<span
<span
sides
<span
paces
<p>You should do this consistently within a single file, so, when modifying an e
xisting file, use the style in that file.</p>
<span class="pl-c">// Y
<span class="pl-c">// r
pan class="pl-en">foo</span> {
<span class="pl-k">namespace</span> <span class="pl-en">bar</span> {</pre></div>
<h2><a id="user-content-horizontal-whitespace" class="anchor" href="#horizontalwhitespace" aria-hidden="true"><span class="octicon octicon-link"></span></a>Hor
izontal Whitespace</h2>
<p>Use of horizontal whitespace depends on location. Never put trailing whitespa
ce at the end of a line.</p>
<h3><a id="user-content-general" class="anchor" href="#general" aria-hidden="tru
e"><span class="octicon octicon-link"></span></a>General</h3>
<div class="highlight highlight-cpp"><pre><span class="pl-k">void</span> <span c
lass="pl-en">f</span>(<span class="pl-k">bool</span> b) { <span class="pl-c">//
Open braces should always have a space before them.</span>
...
<span class="pl-k">int</span> i = <span class="pl-c1">0</span>; <span class="pl
-c">// Semicolons usually have no space before them.</span>
<span class="pl-k">int</span> x[] = { <span class="pl-c1">0</span> }; <span cla
ss="pl-c">// Spaces inside braces for braced-init-list are</span>
<span class="pl-k">int</span> x[] = {<span class="pl-c1">0</span>};
<span cla
ss="pl-c">// optional. If you use them, put them on both sides!</span>
<span class="pl-c">// Spaces around the colon in inheritance and initializer lis
ts.</span>
<span class="pl-k">class</span> <span class="pl-en">Foo</span> : <span class="pl
-k">public</span> <span class="pl-en">Bar</span> {
<span class="pl-k">public:</span>
<span class="pl-c">// For inline function implementations, put spaces betwee
n the braces</span>
<span class="pl-c">// and the implementation itself.</span>
<span class="pl-en">Foo</span>(<span class="pl-k">int</span> b) : Bar(), baz
_(b) {} <span class="pl-c">// No spaces inside empty braces.</span>
<span class="pl-k">void</span> <span class="pl-en">Reset</span>() { baz_ = <
span class="pl-c1">0</span>; } <span class="pl-c">// Spaces separating braces f
rom implementation.</span>
...</pre></div>
<p>Adding trailing whitespace can cause extra work for others editing the same f
ile, when they merge, as can removing existing trailing whitespace. So: Don't in
troduce trailing whitespace. Remove it if you're already changing that line, or
do it in a separate clean-up operation (preferably when no-one else is working o
n the file).</p>
<h3><a id="user-content-loops-and-conditionals" class="anchor" href="#loops-andconditionals" aria-hidden="true"><span class="octicon octicon-link"></span></a>L
oops and Conditionals</h3>
<div class="highlight highlight-cpp"><pre><span class="pl-k">if</span> (b) {
<span class="pl-c">// Space after the keyword in conditions and loops.</sp
an>
} <span class="pl-k">else</span> {
<span class="pl-c">// Spaces around
else.</span>
}
<span class="pl-k">while</span> (test) {} <span class="pl-c">// There is usual
ly no space inside parentheses.</span>
<span class="pl-k">switch</span> (i) {
<span class="pl-k">for</span> (<span class="pl-k">int</span> i = <span class="pl
-c1">0</span>; i < <span class="pl-c1">5</span>; ++i) {
<span class="pl-k">switch</span> ( i ) {
<span class="pl-c">// Loops and cond
itions may have spaces inside</span>
<span class="pl-k">if</span> ( test ) {
<span class="pl-c">// parentheses, b
ut this is rare. Be consistent.</span>
<span class="pl-k">for</span> ( <span class="pl-k">int</span> i = <span class="p
l-c1">0</span>; i < <span class="pl-c1">5</span>; ++i ) {
<span class="pl-k">for</span> ( ; i < <span class="pl-c1">5</span> ; ++i) {
<span class="pl-c">// For loops always have a space after the</span>
...
<span class="pl-c">// semicolon, and may have a space be
fore the</span>
<span class="pl-c">// semicolon.</span>
<span class="pl-k">for</span> (<span class="pl-k">auto</span> x : counts) { <sp
an class="pl-c">// Range-based for loops always have a</span>
...
<span class="pl-c">// space before and after the colon.
</span>
}
<span class="pl-k">switch</span> (i) {
<span class="pl-k">case</span> <span class="pl-c1">1</span>:
<span c
lass="pl-c">// No space before colon in a switch case.</span>
...
<span class="pl-k">case</span> <span class="pl-c1">2</span>: <span class="pl
-k">break</span>; <span class="pl-c">// Use a space after a colon if there's co
de after it.</span></pre></div>
<h3><a id="user-content-operators" class="anchor" href="#operators" aria-hidden=
"true"><span class="octicon octicon-link"></span></a>Operators</h3>
<div class="highlight highlight-cpp"><pre>x = <span class="pl-c1">0</span>;
<span class="pl-c">// Assignment operators always have spaces around</s
pan>
<span class="pl-c">// them.</span>
x = -<span class="pl-c1">5</span>;
<span class="pl-c">// No spaces s
eparating unary operators and their</span>
++x;
<span class="pl-c">// arguments.</span>
<span class="pl-k">if</span> (x && !y)
...
v = w * x + y / z; <span class="pl-c">// Binary operators usually have spaces a
round them,</span>
v = w*x + y/z;
<span class="pl-c">// but it's okay to remove spaces around
factors.</span>
v = w * (x + z);
<span class="pl-c">// Parentheses should have no spaces insi
de them.</span></pre></div>
<h3><a id="user-content-templates-and-casts" class="anchor" href="#templates-and
-casts" aria-hidden="true"><span class="octicon octicon-link"></span></a>Templat
es and Casts</h3>
<div class="highlight highlight-cpp"><pre>vector<string> x;
<spa
n class="pl-c">// No spaces inside the angle</span>
y = <span class="pl-k">static_cast</span><<span class="pl-k">char</span>*>
(x); <span class="pl-c">// brackets (< and >), before</span>
<span class="pl-c">// <, or between >( in a ca
st.</span>
vector<<span class="pl-k">char</span> *> x;
<span class="pl-c">/
/ Spaces between type and pointer are</span>
<span class="pl-c">// okay, but be consistent.</span
>
set<list<string>> x;
<span class="pl-c">// Permitted in C++11
code.</span>
set<list<string> > x;
<span class="pl-c">// C++03 required a s
ecurity, text:security">Security</a></li>
<li><a href="https://github.com/contact" data-ga-click="Footer, go to co
ntact, text:contact">Contact</a></li>
<li><a href="https://help.github.com" data-ga-click="Footer, go to help,
text:help">Help</a></li>
</ul>
</div>
</div>
<div class="fullscreen-overlay js-fullscreen-overlay" id="fullscreen_overlay
">
<div class="fullscreen-container js-suggester-container">
<div class="textarea-wrap">
<textarea name="fullscreen-contents" id="fullscreen-contents" class="fulls
creen-contents js-fullscreen-contents" placeholder="" aria-label=""></textarea>
<div class="suggester-container">
<div class="suggester fullscreen-suggester js-suggester js-navigation-co
ntainer"></div>
</div>
</div>
</div>
<div class="fullscreen-sidebar">
<a href="#" class="exit-fullscreen js-exit-fullscreen tooltipped tooltippedw" aria-label="Exit Zen Mode">
<span class="mega-octicon octicon-screen-normal"></span>
</a>
<a href="#" class="theme-switcher js-theme-switcher tooltipped tooltipped-w"
aria-label="Switch themes">
<span class="octicon octicon-color-mode"></span>
</a>
</div>
</div>