GoogleC++StyleGuideRevision3.180BenjyWeinbergerCraigSilversteinGregoryEitzmannMarkMentovaiTashanaLandrayEachstylepointhasasummaryforwhichadditionalinformationisavailablebytogglingtheaccompanyingarrowbuttonthatlooksthisway:▽.Youmaytoggleallsummarieswiththebigarrowbutton:▽ToggleallsummariesTableofContentsHeaderFilesThe#defineGuardHeaderFileDependenciesInlineFunctionsThe-inl.hFilesFunctionParameterOrderingNamesandOrderofIncludesScopingNamespacesNestedClassesNonmember,StaticMember,andGlobalFunctionsLocalVariablesStaticandGlobalVariablesClassesDoingWorkinConstructorsDefaultConstructorsExplicitConstructorsCopyConstructorsStructsvs.ClassesInheritanceMultipleInheritanceInterfacesOperatorOverloadingAccessControlDeclarationOrderWriteShortFunctionsGoogle-SpecificMagicSmartPointerscpplintOtherC++FeaturesReferenceArgumentsFunctionOverloadingDefaultArgumentsVariable-LengthArraysandalloca()FriendsExceptionsRun-TimeTypeInformation(RTTI)CastingStreamsPreincrementandPredecrementUseofconstIntegerTypes64-bitPortabilityPreprocessorMacros0andNULLsizeofBoostC++0xNamingGeneralNamingRulesFileNamesTypeNamesVariableNamesConstantNamesFunctionNamesNamespaceNamesEnumeratorNamesMacroNamesExceptionstoNamingRulesCommentsCommentStyleFileCommentsClassCommentsFunctionCommentsVariableCommentsImplementationCommentsPunctuation,SpellingandGrammarTODOCommentsDeprecationCommentsFormattingLineLengthNon-ASCIICharactersSpacesvs.TabsFunctionDeclarationsandDefinitionsFunctionCallsConditionalsLoopsandSwitchStatementsPointerandReferenceExpressionsBooleanExpressionsReturnValuesVariableandArrayInitializationPreprocessorDirectivesClassFormatConstructorInitializerListslink▽NamespaceFormattingHorizontalWhitespaceVerticalWhitespaceExceptionstotheRulesExistingNon-conformantCodeWindowsCodeImportantNoteDisplayingHiddenDetailsinthisGuideThisstyleguidecontainsmanydetailsthatareinitiallyhiddenfromview.Theyaremarkedbythetriangleicon,whichyouseehereonyourleft.Clickitnow.YoushouldseeHoorayappearbelow.Hooray!Nowyouknowyoucanexpandpointstogetmoredetails.Alternatively,there'sanexpandallatthetopofthisdocument.BackgroundC++isthemaindevelopmentlanguageusedbymanyofGoogle'sopen-sourceprojects.AseveryC++programmerknows,thelanguagehasmanypowerfulfeatures,butthispowerbringswithitcomplexity,whichinturncanmakecodemorebug-proneandhardertoreadandmaintain.Thegoalofthisguideistomanagethiscomplexitybydescribingindetailthedosanddon'tsofwritingC++code.TheserulesexisttokeepthecodebasemanageablewhilestillallowingcoderstouseC++languagefeaturesproductively.Style,alsoknownasreadability,iswhatwecalltheconventionsthatgovernourC++code.ThetermStyleisabitofamisnomer,sincetheseconventionscoverfarmorethanjustsourcefileformatting.Onewayinwhichwekeepthecodebasemanageableisbyenforcingconsistency.Itisveryimportantthatanyprogrammerbeabletolookatanother'scodeandquicklyunderstandit.Maintainingauniformstyleandfollowingconventionsmeansthatwecanmoreeasilyusepattern-matchingtoinferwhatvarioussymbolsareandwhatinvariantsaretrueaboutthem.Creatingcommon,requiredidiomsandpatternsmakescodemucheasiertounderstand.Insomecasestheremightbegoodargumentsforchangingcertainstylerules,butwenonethelesskeepthingsastheyareinordertopreserveconsistency.AnotherissuethisguideaddressesisthatofC++featurebloat.C++isahugelanguagewithmanyadvancedfeatures.Insomecasesweconstrain,orevenban,useofcertainfeatures.Wedothistokeepcodesimpleandtoavoidthevariouscommonerrorsandproblemsthatthesefeaturescancause.Thisguideliststhesefeaturesandexplainswhytheiruseisrestricted.Open-sourceprojectsdevelopedbyGoogleconformtotherequirementsinthisguide.NotethatthisguideisnotaC++tutorial:weassumethatthereaderisfamiliarwiththelanguage.HeaderFilesIngeneral,every.ccfileshouldhaveanassociated.hfile.Therearesomecommonexceptions,suchasunittestsandsmall.ccfilescontainingjustamain()function.Correctuseofheaderfilescanmakeahugedifferencetothereadability,sizeandperformanceofyourcode.Thefollowingruleswillguideyouthroughthevariouspitfallsofusingheaderfiles.link▽link▽link▽The#defineGuardAllheaderfilesshouldhave#defineguardstopreventmultipleinclusion.TheformatofthesymbolnameshouldbePROJECT_PATH_FILE_H_.Toguaranteeuniqueness,theyshouldbebasedonthefullpathinaproject'ssourcetree.Forexample,thefilefoo/src/bar/baz.hinprojectfooshouldhavethefollowingguard:#ifndefFOO_BAR_BAZ_H_#defineFOO_BAR_BAZ_H_...#endif//FOO_BAR_BAZ_H_HeaderFileDependenciesDon'tusean#includewhenaforwarddeclarationwouldsuffice.Whenyouincludeaheaderfileyouintroduceadependencythatwillcauseyourcodetoberecompiledwhenevertheheaderfilechanges.Ifyourheaderfileincludesotherheaderfiles,anychangetothosefileswillcauseanycodethatincludesyourheadertoberecompiled.Therefore,weprefertominimizeincludes,particularlyincludesofheaderfilesinotherheaderfiles.Youcansignificantlyminimizethenumberofheaderfilesyouneedtoincludeinyourownheaderfilesbyusingforwarddeclarations.Forexample,ifyourheaderfileusestheFileclassinwaysthatdonotrequireaccesstothedeclarationoftheFileclass,yourheaderfilecanjustforwarddeclareclassFile;insteadofhavingto#