您好,欢迎访问三七文档
当前位置:首页 > IT计算机/网络 > 数据库 > 谷歌C++编码规范 Google C++ Style Guide
GoogleC++StyleGuideRevision3.180BenjyWeinbergerCraigSilversteinGregoryEitzmannMarkMentovaiTashanaLandrayEachstylepointhasasummaryforwhichadditionalinformationisavailablebytogglingtheaccompanyingarrowbuttonthatlooksthisway:▽.Youmaytoggleallsummarieswiththebigarrowbutton:▽ToggleallsummariesTableofContentsHeaderFilesThe#defineGuardHeaderFileDependenciesInlineFunctionsThe-inl.hFilesFunctionParameterOrderingNamesandOrderofIncludesScopingNamespacesNestedClassesNonmember,StaticMember,andGlobalFunctionsLocalVariablesStaticandGlobalVariablesClassesDoingWorkinConstructorsDefaultConstructorsExplicitConstructorsCopyConstructorsStructsvs.ClassesInheritanceMultipleInheritanceInterfacesOperatorOverloadingAccessControlDeclarationOrderWriteShortFunctionsGoogle-SpecificMagicSmartPointerscpplintOtherC++FeaturesReferenceArgumentsFunctionOverloadingDefaultArgumentsVariable-LengthArraysandalloca()FriendsExceptionsRun-TimeTypeInformation(RTTI)CastingStreamsPreincrementandPredecrementUseofconstIntegerTypes64-bitPortabilityPreprocessorMacros0andNULLsizeofBoostC++0xNamingGeneralNamingRulesFileNamesTypeNamesVariableNamesConstantNamesFunctionNamesNamespaceNamesEnumeratorNamesMacroNamesExceptionstoNamingRulesCommentsCommentStyleFileCommentsClassCommentsFunctionCommentsVariableCommentsImplementationCommentsPunctuation,SpellingandGrammarTODOCommentsDeprecationCommentsFormattingLineLengthNon-ASCIICharactersSpacesvs.TabsFunctionDeclarationsandDefinitionsFunctionCallsConditionalsLoopsandSwitchStatementsPointerandReferenceExpressionsBooleanExpressionsReturnValuesVariableandArrayInitializationPreprocessorDirectivesClassFormatConstructorInitializerListsNamespaceFormattingHorizontalWhitespaceVerticalWhitespaceExceptionstotheRulesExistingNon-conformantCodeWindowsCodeImportantNoteDisplayingHiddenDetailsinthisGuidelink▽Thisstyleguidecontainsmanydetailsthatareinitiallyhiddenfromview.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.The#defineGuardlink▽Allheaderfilesshouldhave#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_HeaderFileDependencieslink▽Don'tusean#includewhenaforwarddeclarationwouldsuffice.Whenyouincludeaheaderfileyouintroduceadependencythatwillcauseyourcodetoberecompiledwhenevertheheaderfilechanges.Ifyourheaderfileincludesotherheaderfiles,anychangetothosefileswillcauseanycodethatincludesyourheadertoberecompiled.Therefore,weprefertominimizeincludes,particularlyincludesofheaderfilesinotherheaderfiles.Youcansignificantlyminimizethenumberofheaderfilesyouneedtoincludeinyourownheaderfilesbyusingforwarddeclarations.Forexample,ifyourheaderfileusestheFileclassinwaysthatdonotrequireaccesstothedeclarationoftheFileclass,yourheaderfilecanjustforwarddeclareclassFile;insteadofhavingto#inclu
本文标题:谷歌C++编码规范 Google C++ Style Guide
链接地址:https://www.777doc.com/doc-5401335 .html