From 019d40fa03d9135e7db9d04c2c132af5a05e3dc2 Mon Sep 17 00:00:00 2001 From: Nico Weber Date: Tue, 23 Sep 2014 13:21:59 -0700 Subject: Add a document describing which C++11 features we allow. For now, this allows only a very limited set. Also describes the process we're going to follow to allow more features. The idea is that we're gradually going to allow more features over time. Also included is the source of a tiny appengine app running at chromium-cpp.appspot.com that will show the contents of the document in this CL once it's submitted. BUG=360096 NOTRY=true R=ben@chromium.org, inferno@chromium.org, jamesr@chromium.org Review URL: https://codereview.chromium.org/589413003 Cr-Commit-Position: refs/heads/master@{#296214} --- styleguide/c++/c++11.html | 963 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 963 insertions(+) create mode 100644 styleguide/c++/c++11.html (limited to 'styleguide/c++/c++11.html') diff --git a/styleguide/c++/c++11.html b/styleguide/c++/c++11.html new file mode 100644 index 0000000..8370774 --- /dev/null +++ b/styleguide/c++/c++11.html @@ -0,0 +1,963 @@ + + + + + + + + + +
+

C++11 use in Chromium

+ +

This document lives at src/styleguide/c++/c++11.html in a Chromium +checkout.

+ +

This document summarizes the features of C++11 (both in the language itself +and in enhancements to the Standard Library) and describes which features are +allowed in Chromium and contains pointers to more detailed information. The +Guide applies to Chromium and its subprojects. Subprojects can choose to be +more restrictive if they need to compile on more toolchains than Chromium.

+ +

You can propose to make a feature available or to ban a +feature by sending an email to chromium-dev. Ideally include a short blurb +on what the feature is, and why you think it should or should not be allowed. +Ideally, the list will arrive at some consensus and the wiki page will be +updated to mention that consensus. If there's no consensus, +src/styleguide/C++/OWNERS get to decide -- for divisive features, we expect +the decision to be to not use the feature yet and possibly discuss it again a +few months later, when we have more experience with the language.

+ +

Unless otherwise noted, no C++11 +library features are allowed.

+ +

C++11 Allowed Features

+ +

The following features are currently allowed.

+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
FeatureSnippetDescriptionDocumentation LinkNotes and Discussion Thread
Angle Bracket Parsing in Templates>> for > > and
+<:: for < ::
More intuitive parsing of template parameters +C++ Templates Angle Brackets PitfallRecommended to increase readability. Approved without discussion.
Local Types as Template ArgumentsAllows local and unnamed types as template arguments +Local types, types without linkage and unnamed types as template argumentsUsage should be rare. Approved without discussion.
Standard IntegersTypedefs within <stdint.h> +and <inttypes>Provides fixed-size integers independent of platforms +<stdint.h> (cstdint)Already in common use in the codebase. Approved without discussion.
+ +

C++11 Blacklist (Disallowed and Banned Features)

+ +

This section lists features that are not allowed to be used yet. + +

C++11 Banned Features

+ +

None yet! This section will list C++11 features that are not allowed in the +Chromium codebase. +

+ + + +

C++11 Features To Be Discussed

+ +

The following C++ language features are currently disallowed. +See the top of this page on how to propose moving a feature from this list +into the allowed or banned sections. Note that not all of these features +work in all our compilers yet.

+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
FeatureSnippetDescriptionDocumentation LinkNotes
Aliasesusing new_alias = typenameAllow parameterized typedefsType alias (using syntax)
Alignment Features +alignas specifier, +std::alignment_of<T>, +std::aligned_union<Size, ...Types> and +std::max_align_tObject alignmentstd::alignment_of
Attributes[[attribute_name]]Attaches properties to declarations that +specific compiler implementations may use. +C++11 generalized attributes
Automatic TypesautoAutomatic type deduction +Tip of the Week #4: Use const auto& and +auto to replace complex declarationsGoogle +Style Guide on auto
Constant ExpressionsconstexprCompile-time constant expressions +Tip of the Week #57: constexpr is foreverNot supported in MSVS2013. Google +Style Guide on constexpr
Declared Type Accessordecltype(expression)Provides a means to determine the type of an expression at compile-time, +useful most often in templates. +decltype specifier
Default Function CreationFunction(arguments) = default;Instructs the compiler to generate a default version +of the indicated function +What's the point in defaulting functions in C++11?
Default Function Template Argumentstemplate <typename T = type>
+  type Function(T var) {}
Allow function templates, like classes, to have default arguments +Default Template Arguments for Function Templates
Delegated ConstructorsClass() : Class(0) {}
+Class(type var) : Class(var, 0)
Allow overloaded constructors to use common initialization code +Introduction to the C++11 feature: delegating constructors
Enumerated Type Classesenum class classnameProvide enums as full classes, with no implicit +conversion to booleans or integers +enum-class
Exception Featuresnoexcept, exception_ptr, +current_exception(), rethrow_exception, +and nested_exceptionEnhancements to exception throwing and handling +std::exceptionExceptions are banned by the + +C++ Style Guide.
Explicit Conversion Operatorsexplicit operator type() { +
  // code
}
Allows conversion operators that cannot be implicitly invoked +explicit specifier
Final DeclarationsfinalIndicates that a class or function is final and cannot be overridden +Tip of the Week #54: Controlling Your InheritanceFINAL is already widely used in the codebase.
Function Local Variable__func__Provides a local variable of the name of the enclosing +function for logging purposes +The __func__ Predeclared Identifier is Coming to C++
Function SuppressionFunction(arguments) = delete;Suppresses the implementation of a function, especially a +synthetic function such as a copy constructorTODO: documentation link
(Uniform) Initialization Syntaxtype name { [value ..., value]};Allows any object of primitive, aggregate or class +type to be initialized using brace syntax +Tip of the Week #58: Initializer Lists and the Uniform Initialization Syntax
Inline NamespacesinlineAllows better versioning of namespacesNamespacesUnder investigation, unclear how it will work with +components
Lambda Expressions[captures](params) -> ret { body }Anonymous functionsLambda functionsNo default captures (Google Style Guide).
long long Typelong long var= value;An integer of at least 64 bits +Fundamental types
Non-Static Class Member Initializers + +class C {
+ type var = value;
+ C() // copy-initializes var
+
+
Allows non-static class members to be initialized at their definitions (outside constructors) +Tip of the Week #58: Non-Static Class Member Initializers and + +Non-static data members
Null Pointer ConstantnullptrDeclares a type-safe null pointer +Tip of the Week #39: Prefer C++11’s nullptr to NULL or 0Google Style Guidwe. +
OverridesoverrideIndicates that a class or function overrides a base implementation +Tip of the Week #54: Controlling Your InheritanceOVERRIDE is already widely used in the codebase.
Range-Based For Loopsfor (type var : range)Facilitates a more concise syntax for iterating over the elements +of a container (or a range of iterators) in a for loopTODO: documentation link/a>
Raw String Literalsstring var=R"(raw_string)";Allows a string to be encoded without any escape +sequences, easing parsing in regex expressions, for exampleTODO: documentation linkRaw String Literals
Rvalue References (and Move Semantics)T(T&& t) and T& operator=(T&& t)Reference that only binds to a temporary objectTODO: documentation linkstruction and assignment
Static Assertionsstatic_assert(bool, string)Tests compile-time conditions<Static Assertion
Union Class Membersunion name { class var}Allows class type members +Union declarations
User-Defined Literalstype var = literal_value_typeAllows user-defined literal expressionsTODO: documentation link
UTF-8 String Literalsu8"string"Enforces UTF-8 encoding on all string literals +string literal
UTF-16 and UTF-32 Support (16-Bit and 32-Bit Character Types)char16_t and char32_tProvides character types for handling 16-bit +and 32-bit code units (useful for encoding +UTF-16 and UTF-32 string data) +Fundamental typesNon-UTF-8 text is banned by the + +C++ Style Guide. However, may be useful for +consuming non-ASCII data.
Variadic Macros#define MACRO(...) Impl(args, __VA_ARGS__)Allows macros that accept a variable number of arguments +Are Variadic macros nonstandard?
Variadic Templatestemplate <typename ... arg>Allows templates that accept a variable number of argumentsTODO: documentation link
+ +

C++11 Standard Library features

+ +
+ +

All C++11 Standard Library features are currently +banned, because they are not supported on all of our toolchains yet. +In particular, chromium/android is currently using STLport, and chromium/mac is +currently using libstdc++4.2, which predate C++11. +

+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
FeatureSnippetDescriptionDocumentation LinkStyle Guide Usage
Address Retrievalstd::addressof()Obtains the address of an object even with overloaded operator&std::addressofUsage should be rare as + +Operator Overloading is rare and &s; +should suffice in most cases. May be preferable +over &s; for performing object +identity checks.
Aligned Storagestd::aligned_storage<Size, Align>::typeDeclare uninitialized storage having a specified alignment.std::aligned_storageNote: std::aligned_storage is allowed, but some other C++11 +alignment features are still disallowed.
Allocator Traitsstd::allocator_traitsProvides an interface for accessing custom allocators +std::allocator_traitsUsage should be rare.
Atomicsstd::atomic and others in <atomic>Fine-grained atomic types and operations<atomic>
Arraysstd::arrayA fixed-size replacement for built-in arrays, with STL support +std::array
Begin and End Non-Member Functionsstd::begin() and std::end()Allows use of free functions on any container, including +built-in arrays +std::begin and + +std::endUseful for built-in arrays.
Bind Operationsstd::bind(function, args, ...)Declares a function object bound to certain argumentsTODO: documentation link
C Floating-Point Environment<cfenv>Provides floating point status flags and control modes for C-compatible codeStandard library header <cfenv>Compilers do not support use
Chrono Library<chrono>Provides a standard date and time libraryDate and time utilities
Complex Inverse Trigonometric and Hyperbolic FunctionsFunctions within <complex>Adds inverse trigonomentric and hyperbolic non-member functions to +the <complex> library.std::complex
Conditional Type Selectionstd::enable_if and std::conditionalEnables compile-time conditional type selection +std::enable_if and + +conditional
Constant Iterator Methods on Containersstd::cbegin() and std::cend()Enforces iteration methods that don't change container contentsTODO: documentation link
Construct Elements in Containersemplace(),emplace_back(), +emplace_front(), emplace_hint()Constructs elements directly within a container without a copy +or a moveTODO: documentation linkUse where element construction within a container +is needed.
Container Compaction Functionsstd::vector::shrink_to_fit(), +std::deque::shrink_to_fit(), and +std::string::shrink_to_fit()Requests the removal of unused space +in the container +std::vector::shrink_to_fit, + +std::deque::shrink_to_fit, and + +std::basic_string::shrink_to_fit
Date/Time String Formatting Specifiersstd::strftime()Converts date and time information into a +formatted string using new specifiers +std::strftime
Function Return Type Deductionstd::result_of<Functor(ArgTypes...)>Extracts the return type from the type signature of +a function call invocation at compile-time. +std::result_of + +Why does std::result_of take an (unrelated) function type as a type argument? +
Function Objectsstd::functionWraps a standard polymorphic functionTODO: documentation link
Forward Listsstd::forward_listProvides an efficient singly linked list +std::forward_list
Gamma Natural Logstd::lgamma()Computes the natural log of the gamma of a +floating point value +std::lgamma
Garbage Collection Featuresstd::{un}declare_reachable() and +std::{un}declare_no_pointers()Enables garbage collection implementations +std::declare_reachable +and +std::declare_no_pointers
Heap Validationstd::is_heap()Checks whether an iterator range is in fact a heapTODO: documentation link
Is Nanstd::isnan()Determines if a floating point value is not-a-numberstd::isnan
Iterator Operatorsstd::next() and std::prev()Copies an iterator and increments or decrements the copy by +some valuestd::next +and std::prev +
Initializer Listsstd::initializer_list<T>Allows containers to be initialized with aggregate elements +Tip of the Week #58: Initializer Lists and the Uniform Initialization Syntax
Move Semanticsstd::move()Facilitates efficient move operations +std::move reference
Pointer Traits Class Templatestd::pointer_traitsProvides a standard way to access properties +of pointers and pointer-like types +std::pointer_traitsUseful for determining the element type +pointed at by a (possibly smart) pointer.
Random Number GeneratorsFunctions within <random>Random number generation algorithms and utilities +Pseudo-random number generation
Ratio Template Classstd::ratio<numerator, denominator>Provides compile-time rational numbersTODO: documentation link
Reference Wrapper Classesstd::reference_wrapper and +std::ref(), std::cref()Allows you to wrap a reference within a standard +object (and use those within containers) +Reference Wrappers
Regex Library<regex>Provides a standard regular expressions libraryRegular expressions library
Shared Pointersstd::shared_ptrAllows shared ownership of a pointer through reference countsstd::shared_ptr +Ownership and Smart Pointers
Soft Program Exitsstd::at_quick_exit() +and std::quick_exit()Allows registration of functions to be called upon exit, +allowing cleaner program exit than abort() or +exit +std:quick_exit
String Direct Reference Functionsstd::string::front() and std::string::back()Returns a reference to the front or back of a string +std::basic_string::front and + +std::basic_string::back
String to Number Functionsstd::stoi(), std::stol(), +std::stoul(), std::stoll, std::stoull(), +std::stof(), std::stod(), std::stold(), +and std::to_string()Converts strings to numbers +std::stoi, std::stol, std::stoll, + +std::stoul, std::stoull, and + +std::stof, std::stod, std::stold
STL AlgorithmsFunctions within <algorithm>.Enhancements to the set of STL algorithms +Tip of the Week #21: New C++11 STL Algorithms. See +the +Algorithms library for a complete list.
System Errors<system_error>Provides a standard system error librarystd::system_error
Trailing Return Typesauto function declaration -> return_typeAllows trailing function return value syntax +Declaring functions
Thread Library<thread> support, including <future>, +<mutex>, <condition_variable>Provides a standard mulitthreading library using std::thread and associatesThread support library
Tuplesstd::tupleA fixed-size ordered collection of values of mixed typesTODO: documentation link
Type-Generic Math FunctionsFunctions within <ctgmath>Provides a means to call real or complex functions +based on the type of arguments +Standard library header <ctgmath>
Type Info Enhancementsstd::type_index and std::type_info::hash_code()Allows type information (most often within containers) +that can be copied, assigned, or hashed +std::type_index and + +std::type_info::hash_codestd::type_index is a thin wrapper for +std::type_info, allowing you to use it directly +within both associative and unordered containers
Type TraitsClass templates within <type_traits>Allows compile-time inspection of the properties of types +Standard library header <type_traits>
Unique Pointersstd::unique_ptr<type>Defines a pointer with clear and unambiguous ownershipTODO: documentation link +Ownership and Smart Pointers
Unordered Associative Containersstd::unordered_set, std::unordered_map, +std::unordered_multiset, and std::unordered_multimapAllows efficient containers of key/value pairsstd::unordered_map +and std::unordered_set +
Variadic Copy Macrova_copy(va_list dest, va_list src)Makes a copy of the variadic function arguments
Weak Pointersstd::weak_ptrAllows a weak reference to a std::shared_ptr +std::weak_ptr +Ownership and Smart Pointers
Wide String Supportstd::wstring_convert, +std::wbuffer_convert. +std::codecvt_utf8, std::codecvt_utf16, +and std::codecvt_utf8_utf16Converts between string encodings +std::wstring_convert, + +std::wbuffer_convert, + +std::codecvt_utf8, + +std::codecvt_utf16, and + +std::codecvt_utf8_utf16 +Non-UTF-8 text is banned by the + +C++ Style Guide. However, may be useful for +consuming non-ASCII data.
+ +
+ +
+ + -- cgit v1.1