Windows 7 32 Y 64 Bits Todo En Uno

0 views
Skip to first unread message

Roser Blazado

unread,
Jun 30, 2024, 6:46:08 AM (2 days ago) Jun 30
to tysanbitee

C++ is one of the main development languages used bymany of Google's open-source projects. As every C++programmer knows, the language has many powerful features, butthis power brings with it complexity, which in turn can makecode more bug-prone and harder to read and maintain.

The goal of this guide is to manage this complexity bydescribing in detail the dos and don'ts of writing C++code. These rules exist tokeep the code base manageable while still allowingcoders to use C++ language features productively.

Style, also known as readability, is what we callthe conventions that govern our C++ code. The term Style is abit of a misnomer, since these conventions cover far more thanjust source file formatting.

There are a few core goals that we believe this guide shouldserve. These are the fundamental whys thatunderlie all of the individual rules. By bringing these ideas tothe fore, we hope to ground discussions and make it clearer to ourbroader community why the rules are in place and why particulardecisions have been made. If you understand what goals each rule isserving, it should be clearer to everyone when a rule may be waived(some can be), and what sort of argument or alternative would benecessary to change a rule in the guide.

The intent of this document is to provide maximal guidance withreasonable restriction. As always, common sense and good taste shouldprevail. By this we specifically refer to the established conventionsof the entire Google C++ community, not just your personal preferencesor those of your team. Be skeptical about and reluctant to useclever or unusual constructs: the absence of a prohibition is not thesame as a license to proceed. Use your judgment, and if you areunsure, please don't hesitate to ask your project leads to get additionalinput.

All header files should be self-contained. Users and refactoringtools should not have to adhere to special conditions to include theheader. Specifically, a header shouldhave header guards and include allother headers it needs.

When a header declares inline functions or templates that clients of theheader will instantiate, the inline functions and templates must also havedefinitions in the header, either directly or in files it includes. Do not movethese definitions to separately included header (-inl.h) files;this practice was common in the past, but is no longer allowed. When allinstantiations of a template occur in one .cc file, either becausethey're explicit or because the definition is accessible to onlythe .cc file, the template definition can be kept in that file.

There are rare cases where a file designed to be included is notself-contained. These are typically intended to be included at unusuallocations, such as the middle of another file. They might notuse header guards, and might not includetheir prerequisites. Name such files with the .incextension. Use sparingly, and prefer self-contained headers whenpossible.

If a source or header file refers to a symbol defined elsewhere,the file should directly include a header file which properly intendsto provide a declaration or definition of that symbol. It should notinclude header files for any other reason.

Do not rely on transitive inclusions. This allows people to removeno-longer-needed #include statements from their headers withoutbreaking clients. This also applies to related headers- foo.cc should include bar.h if it uses asymbol from it even if foo.hincludes bar.h.

Overuse of inlining can actually make programs slower.Depending on a function's size, inlining it can cause thecode size to increase or decrease. Inlining a very smallaccessor function will usually decrease code size whileinlining a very large function can dramatically increasecode size. On modern processors smaller code usually runsfaster due to better use of the instruction cache.

It is important to know that functions are not alwaysinlined even if they are declared as such; for example,virtual and recursive functions are not normally inlined.Usually recursive functions should not be inline. Themain reason for making a virtual function inline is toplace its definition in the class, either for convenienceor to document its behavior, e.g., for accessors andmutators.

All of a project's header files should belisted as descendants of the project's sourcedirectory without use of UNIX directory aliases. (the current directory) or ..(the parent directory). For example,google-awesome-project/src/base/logging.hshould be included as:

With the preferred ordering, if the related headerdir2/foo2.h omits any necessaryincludes, the build of dir/foo.ccor dir/foo_test.cc will break.Thus, this rule ensures that build breaks show up firstfor the people working on these files, not for innocentpeople in other packages.

With few exceptions, place code in a namespace. Namespacesshould have unique names based on the project name, and possiblyits path. Do not use using-directives (e.g.,using namespace foo). Do not useinline namespaces. For unnamed namespaces, seeInternal Linkage.

For example, if two different projects have a classFoo in the global scope, these symbols maycollide at compile time or at runtime. If each projectplaces their code in a namespace, project1::Fooand project2::Foo are now distinct symbols thatdo not collide, and code within each project's namespacecan continue to refer to Foo without the prefix.

Do not use Namespace aliases at namespace scope in header files except in explicitly marked internal-only namespaces, because anything imported into a namespace in a header file becomes part of the public API exported by that file.

When definitions in a .cc file do not need to bereferenced outside that file, give them internal linkage by placingthem in an unnamed namespace or declaring them static.Do not use either of these constructs in .h files.

All declarations can be given internal linkage by placing them in unnamednamespaces. Functions and variables can also be given internal linkage bydeclaring them static. This means that anything you're declaringcan't be accessed from another file. If a different file declares something withthe same name, then the two entities are completely independent.

Prefer placing nonmember functions in a namespace; use completely globalfunctions rarely. Do not use a class simply to group static members. Staticmethods of a class should generally be closely related to instances of theclass or the class's static data.

Sometimes it is useful to define afunction not bound to a class instance. Such a functioncan be either a static member or a nonmember function.Nonmember functions should not depend on externalvariables, and should nearly always exist in a namespace.Do not create classes only to group static members;this is no different than just giving the names acommon prefix, and such grouping is usually unnecessary anyway.

C++ allows you to declare variables anywhere in a function.We encourage you to declare them in a scope as local aspossible, and as close to the first use as possible.This makes it easier for the reader to find thedeclaration and see what type the variable is and what itwas initialized to. In particular, initialization shouldbe used instead of declaration and assignment, e.g.,:

Objects withstatic storage duration are forbidden unless they aretriviallydestructible. Informally this means that the destructor does not doanything, even taking member and base destructors into account. More formally itmeans that the type has no user-defined or virtual destructor and that all basesand non-static members are trivially destructible.Static function-local variables may use dynamic initialization.Use of dynamic initialization for static class member variables or variables atnamespace scope is discouraged, but allowed in limited circumstances; see belowfor details.

Every object has a storage duration, which correlates with itslifetime. Objects with static storage duration live from the point of theirinitialization until the end of the program. Such objects appear as variables atnamespace scope ("global variables"), as static data members of classes, or asfunction-local variables that are declared with the staticspecifier. Function-local static variables are initialized when control firstpasses through their declaration; all other objects with static storage durationare initialized as part of program start-up. All objects with static storageduration are destroyed at program exit (which happens before unjoined threadsare terminated).

Initialization may be dynamic, which means that somethingnon-trivial happens during initialization. (For example, consider a constructorthat allocates memory, or a variable that is initialized with the currentprocess ID.) The other kind of initialization is staticinitialization. The two aren't quite opposites, though: staticinitialization always happens to objects with static storage duration(initializing the object either to a given constant or to a representationconsisting of all bytes set to zero), whereas dynamic initialization happensafter that, if required.

Global and static variables are very useful for a large number ofapplications: named constants, auxiliary data structures internal to sometranslation unit, command-line flags, logging, registration mechanisms,background infrastructure, etc.

Global and static variables that use dynamic initialization or havenon-trivial destructors create complexity that can easily lead to hard-to-findbugs. Dynamic initialization is not ordered across translation units, andneither is destruction (except that destructionhappens in reverse order of initialization). When one initialization refers toanother variable with static storage duration, it is possible that this causesan object to be accessed before its lifetime has begun (or after its lifetimehas ended). Moreover, when a program starts threads that are not joined at exit,those threads may attempt to access objects after their lifetime has ended iftheir destructor has already run.

When destructors are trivial, their execution is not subject to ordering atall (they are effectively not "run"); otherwise we are exposed to the risk ofaccessing objects after the end of their lifetime. Therefore, we only allowobjects with static storage duration if they are trivially destructible.Fundamental types (like pointers and int) are triviallydestructible, as are arrays of trivially destructible types. Note thatvariables marked with constexpr are trivially destructible.

d3342ee215
Reply all
Reply to author
Forward
0 new messages