Misplaced Pages

C++11

Article snapshot taken from Wikipedia with creative commons attribution-sharealike license. Give it a read and then ask your questions in the chat. We can research this topic together.

C++11 is a version of a joint technical standard , ISO/IEC 14882, by the International Organization for Standardization (ISO) and International Electrotechnical Commission (IEC), for the C++ programming language. C++11 replaced the prior version of the C++ standard, named C++03 , and was later replaced by C++14 . The name follows the tradition of naming language versions by the publication year of the specification, though it was formerly named C++0x because it was expected to be published before 2010.

#103896

82-487: Although one of the design goals was to prefer changes to the libraries over changes to the core language , C++11 does make several additions to the core language. Areas of the core language that were significantly improved include multithreading support, generic programming support, uniform initialization, and performance. Significant changes were also made to the C++ Standard Library , incorporating most of

164-439: A const std::vector<T>& , incurring a significant memory allocation.) For safety reasons, some restrictions are imposed. A named variable will never be considered to be an rvalue even if it is declared as such. To get an rvalue, the function template std::move() should be used. Rvalue references can also be modified only under certain circumstances, being intended to be used primarily with move constructors. Due to

246-565: A function to machine code and execute it at run time , to execute the function at compile time . This is possible if the arguments to the function are known at compile time, and the function does not make any reference to or attempt to modify any global state (i.e. it is a pure function ). If the value of only some of the arguments are known, the compiler may still be able to perform some level of compile-time function execution ( partial evaluation ), possibly producing more optimized code than if no arguments were known. The Lisp macro system

328-521: A base class exists in the inheritance graph, avoiding some of the ambiguity problems of multiple inheritance. Multiple inheritance is a C++ feature allowing a class to be derived from more than one base class; this allows for more elaborate inheritance relationships. For example, a "Flying Cat" class can inherit from both "Cat" and "Flying Mammal". Some other languages, such as C# or Java , accomplish something similar (although more limited) by allowing inheritance of multiple interfaces while restricting

410-604: A class with public and private non-static data members would not be standard-layout, but it could be trivial and thus memcpy -able. In C++03, the compiler must instantiate a template whenever a fully specified template is encountered in a translation unit. If the template is instantiated with the same types in many translation units, this can dramatically increase compile times. There is no way to prevent this in C++03, so C++11 introduced extern template declarations, analogous to extern data declarations. C++03 has this syntax to oblige

492-466: A compiler couldn't accept the program; if someone were to create a C++03 POD type and add a non-virtual member function, this type would no longer be a POD type, could not be statically initialized, and would be incompatible with C despite no change to the memory layout. C++11 relaxed several of the POD rules, by dividing the POD concept into two separate concepts: trivial and standard-layout . A type that

574-709: A constant expression for a given invocation, the result is not a constant expression. constexpr differs from consteval , introduced in C++20 , in that the latter must always produce a compile time constant, while constexpr does not have this restriction. In C++03, a class or struct must follow a number of rules for it to be considered a plain old data (POD) type. Types that fit this definition produce object layouts that are compatible with C, and they could also be initialized statically. The C++03 standard has restrictions on what types are compatible with C or can be statically initialized despite there being no technical reason

656-411: A constant expression. To construct constant expression data values from user-defined types, constructors can also be declared with constexpr . A constexpr constructor's function body can contain only declarations and null statements, and cannot declare variables or define types, as with a constexpr function. There must exist argument values such that, after argument substitution, it initializes

738-449: A constructor or initializer this is used to define the initial state of the object. Local variables are destroyed when the local block or function that they are declared in is closed. C++ destructors for local variables are called at the end of the object lifetime, allowing a discipline for automatic resource management termed RAII , which is widely used in C++. Member variables are created when

820-403: A cost. Template use may increase object code size, because each template instantiation produces a copy of the template code: one for each set of template arguments, however, this is the same or smaller amount of code that would be generated if the code were written by hand. This is in contrast to run-time generics seen in other languages (e.g., Java ) where at compile-time the type is erased and

902-411: A few notable exceptions such as member access ( . and .* ) and the conditional operator. The rich set of overloadable operators is central to making user-defined types in C++ seem like built-in types. Overloadable operators are also an essential part of many advanced C++ programming techniques, such as smart pointers . Overloading an operator does not change the precedence of calculations involving

SECTION 10

#1732772650104

984-403: A new std::vector<T> and copying all the rvalue's data into it. Then the temporary and all its memory is destroyed. (For simplicity, this discussion neglects the return value optimization .) In C++11, a move constructor of std::vector<T> that takes an rvalue reference to an std::vector<T> can copy the pointer to the internal C-style array out of the rvalue into

1066-417: A new, standalone compiler for C++, Cfront . In 1984, Stroustrup implemented the first stream input/output library. The idea of providing an output operator rather than a named output function was suggested by Doug McIlroy (who had previously suggested Unix pipes ). In 1985, the first edition of The C++ Programming Language was released, which became the definitive reference for the language, as there

1148-589: A number of problems with initializing types. Several ways to do this exist, and some produce different results when interchanged. The traditional constructor syntax, for example, can look like a function declaration, and steps must be taken to ensure that the compiler's most vexing parse rule will not mistake it for such. Only aggregates and POD types can be initialized with aggregate initializers (using SomeType var = {/*stuff*/}; ). Core language C++ ( / ˈ s iː p l ʌ s p l ʌ s / , pronounced " C plus plus " and sometimes abbreviated as CPP )

1230-496: A simple way (D version 2): CTFE can be used to generate strings which are then parsed and compiled as D code in D. Here's an example of compile time function evaluation in the Zig programming language : This example specifies a valid Zig function called "factorial" which would typically be evaluated at run time. The use of comptime tells the compiler that the initializer for the variables must be computed at compile time. Note that

1312-892: A single template body is preserved. Templates are different from macros : while both of these compile-time language features enable conditional compilation, templates are not restricted to lexical substitution. Templates are aware of the semantics and type system of their companion language, as well as all compile-time type definitions, and can perform high-level operations including programmatic flow control based on evaluation of strictly type-checked parameters. Macros are capable of conditional control over compilation based on predetermined criteria, but cannot instantiate new types, recurse, or perform type evaluation and in effect are limited to pre-compilation text-substitution and text-inclusion/exclusion. In other words, macros can control compilation flow based on pre-defined symbols but cannot, unlike templates, independently instantiate new symbols. Templates are

1394-491: A successor to C with Classes, which he named "C++" ( ++ being the increment operator in C) after going through several other names. New features were added, including virtual functions , function name and operator overloading , references , constants, type-safe free-store memory allocation (new/delete), improved type checking, and BCPL-style single-line comments with two forward slashes ( // ). Furthermore, Stroustrup developed

1476-444: A template, compilers substitute specific arguments for a template's parameters to generate a concrete function or class instance. Some substitutions are not possible; these are eliminated by an overload resolution policy described by the phrase " Substitution failure is not an error " (SFINAE). Templates are a powerful tool that can be used for generic programming , template metaprogramming , and code optimization, but this power implies

1558-401: A tool for static polymorphism (see below) and generic programming . In addition, templates are a compile-time mechanism in C++ that is Turing-complete , meaning that any computation expressible by a computer program can be computed, in some form, by a template metaprogram before runtime. In summary, a template is a compile-time parameterized function or class written without knowledge of

1640-538: Is standard-layout means that it orders and packs its members in a way that is compatible with C. A class or struct is standard-layout, by definition, provided: A class/struct/union is considered POD if it is trivial, standard-layout, and all of its non-static data members and base classes are PODs. By separating these concepts, it becomes possible to give up one without losing the other. A class with complex move and copy constructors may not be trivial, but it could be standard-layout and thus interoperate with C. Similarly,

1722-517: Is trivial can be statically initialized. It also means that it is valid to copy data around via memcpy , rather than having to use a copy constructor. The lifetime of a trivial type begins when its storage is defined, not when a constructor completes. A trivial class or struct is defined as one that: Constructors are trivial only if there are no virtual member functions of the class and no virtual base classes. Copy/move operations also require all non-static data members to be trivial. A type that

SECTION 20

#1732772650104

1804-469: Is a high-level , general-purpose programming language created by Danish computer scientist Bjarne Stroustrup . First released in 1985 as an extension of the C programming language , it has since expanded significantly over time; as of 1997 , C++ has object-oriented , generic , and functional features, in addition to facilities for low-level memory manipulation for systems like microcomputers or to make operating systems like Linux or Windows . It

1886-535: Is a special kind of constructor, called an initializer-list-constructor. Classes with such a constructor are treated specially during uniform initialization (see below ) The template class std::initializer_list<> is a first-class C++11 standard library type. They can be constructed statically by the C++11 compiler via use of the {} syntax without a type name in contexts where such braces will deduce to an std::initializer_list , or by explicitly specifying

1968-496: Is an early example of the use of compile-time evaluation of user-defined functions in the same language. The Metacode extension to C++ (Vandevoorde 2003) was an early experimental system to allow compile-time function evaluation (CTFE) and code injection as an improved syntax for C++ template metaprogramming . In earlier versions of C++ , template metaprogramming is often used to compute values at compile time, such as: Using compile-time function evaluation, code used to compute

2050-484: Is entered (see exceptions below) and destroyed in reverse order of creation after main() exits. The exact order of creation is not specified by the standard (though there are some rules defined below) to allow implementations some freedom in how to organize their implementation. More formally, objects of this type have a lifespan that "shall last for the duration of the program". Static storage duration objects are initialized in two phases. First, "static initialization"

2132-413: Is fully supported by GNU Compiler Collection (GCC) 4.8.1 and later. The design committee attempted to stick to a number of goals in designing C++11: Attention to beginners is considered important, because most computer programmers will always be such, and because many beginners never widen their knowledge, limiting themselves to work in aspects of the language in which they specialize. One function of

2214-488: Is given a list of arguments in braces, in the order of the members' definitions in the struct. These initializer-lists are recursive, so an array of structs or struct containing other structs can use them. This is very useful for static lists, or initializing a struct to some value. C++ also provides constructors to initialize an object, but they are often not as convenient as the initializer list. However, C++03 allows initializer-lists only on structs and classes that conform to

2296-432: Is not a constant expression. A C++03 compiler has no way of knowing if get_five() actually is constant at runtime. In theory, this function could affect a global variable, call other non-runtime constant functions, etc. C++11 introduced the keyword constexpr , which allows the user to guarantee that a function or object constructor is a compile-time constant. The above example can be rewritten as follows: This allows

2378-399: Is performed, and only after all static initialization is performed, "dynamic initialization" is performed. In static initialization, all objects are first initialized with zeros; after that, all objects that have a constant initialization phase are initialized with the constant expression (i.e. variables initialized with a literal or constexpr ). Though it is not specified in the standard,

2460-458: Is specially treated by the compiler, an std::initializer_list is a real type, and so it can be used in other places besides class constructors. Regular functions can take typed std::initializer_list s as arguments. For example: Examples of this in the standard library include the std::min() and std::max() templates taking std::initializer_list s of numeric type. Standard containers can also be initialized in these ways: C++03 has

2542-685: Is standardized by the International Organization for Standardization (ISO), with the latest standard version ratified and published by ISO in October 2024 as ISO/IEC 14882:2024 (informally known as C++23 ). The C++ programming language was initially standardized in 1998 as ISO/IEC 14882:1998 , which was then amended by the C++03 , C++11 , C++14 , C++17 , and C++20 standards. The current C++23 standard supersedes these with new features and an enlarged standard library . Before

C++11 - Misplaced Pages Continue

2624-400: Is that they have a lifetime that is limited to the scope of the variable. They are created and potentially initialized at the point of declaration (see below for details) and destroyed in the reverse order of creation when the scope is left. This is implemented by allocation on the stack . Local variables are created as the point of execution passes the declaration point. If the variable has

2706-709: Is the basis of C++. Doing it efficiently is what distinguishes it from other languages." C++ inherits most of C's syntax . A hello world program that conforms to the C standard is also a valid C++ hello world program. The following is Bjarne Stroustrup's version of the Hello world program that uses the C++ Standard Library stream facility to write a message to standard output : As in C, C++ supports four types of memory management : static storage duration objects, thread storage duration objects, automatic storage duration objects, and dynamic storage duration objects. Static storage duration objects are created before main()

2788-824: Is usually implemented as a compiled language , and many vendors provide C++ compilers , including the Free Software Foundation , LLVM , Microsoft , Intel , Embarcadero , Oracle , and IBM . C++ was designed with systems programming and embedded , resource-constrained software and large systems in mind, with performance , efficiency, and flexibility of use as its design highlights. C++ has also been found useful in many other contexts, with key strengths being software infrastructure and resource-constrained applications, including desktop applications , video games , servers (e.g., e-commerce , web search , or databases ), and performance-critical applications (e.g., telephone switches or space probes ). C++

2870-535: The C++ Technical Report 1 (TR1) libraries , except the library of mathematical special functions. C++11 was published as ISO/IEC 14882:2011 in September 2011 and is available for a fee. The working draft most similar to the published C++11 standard is N3337, dated 16 January 2012; it has only editorial corrections from the C++11 standard. C++11 is fully supported by Clang 3.3 and later. C++11

2952-542: The Charles Stark Draper Prize for Engineering, "for conceptualizing and developing the C++ programming language". In December 2022, C++ ranked third on the TIOBE index , surpassing Java for the first time in the history of the index. As of November 2024 , the language ranks second after Python , with Java being in third. According to Stroustrup, "the name signifies the evolutionary nature of

3034-413: The D programming language : This example specifies a valid D function called "factorial" which would typically be evaluated at run time. The use of enum tells the compiler that the initializer for the variables must be computed at compile time. Note that the arguments to the function must be able to be resolved at compile time as well. CTFE can be used to populate data structures at compile-time in

3116-440: The C++ committee is the development of the language core. Areas of the core language that were significantly improved include multithreading support, generic programming support, uniform initialization, and performance. These language features primarily exist to provide some kind of runtime performance benefit, either of memory or of computing speed. In C++03 (and before), temporaries (termed " rvalues ", as they often lie on

3198-472: The C++ language , including Linus Torvalds , Richard Stallman , Joshua Bloch , Ken Thompson , and Donald Knuth . In 1979, Bjarne Stroustrup , a Danish computer scientist , began work on " C with Classes ", the predecessor to C++. The motivation for creating a new language originated from Stroustrup's experience in programming for his PhD thesis. Stroustrup found that Simula had features that were very helpful for large software development, but

3280-520: The ISO working group standardized C++ for the first time as ISO/IEC 14882:1998 , which is informally known as C++98 . In 2003, it published a new version of the C++ standard called ISO/IEC 14882:2003 , which fixed problems identified in C++98. The next major revision of the standard was informally referred to as "C++0x", but it was not released until 2011. C++11 (14882:2011) included many additions to both

3362-502: The Plain Old Data (POD) definition; C++11 extends initializer-lists, so they can be used for all classes including standard containers like std::vector . C++11 binds the concept to a template, called std::initializer_list . This allows constructors and other functions to take initializer-lists as parameters. For example: This allows SequenceClass to be constructed from a sequence of integers, such as: This constructor

C++11 - Misplaced Pages Continue

3444-586: The allocated memory. The C++ Core Guidelines advise against using new directly for creating dynamic objects in favor of smart pointers through make_unique < T > for single ownership and make_shared < T > for reference-counted multiple ownership, which were introduced in C++11. C++ templates enable generic programming . C++ supports function, class, alias, and variable templates. Templates may be parameterized by types, compile-time constants, and other templates. Templates are implemented by instantiation at compile-time. To instantiate

3526-508: The changes from C." This name is credited to Rick Mascitti (mid-1983) and was first used in December 1983. When Mascitti was questioned informally in 1992 about the naming, he indicated that it was given in a tongue-in-cheek spirit. The name comes from C's ++ operator (which increments the value of a variable ) and a common naming convention of using "+" to indicate an enhanced computer program. During C++'s development period,

3608-432: The class ("friends"). A protected member is accessible to members of classes that inherit from the class in addition to the class itself and any friends. The object-oriented principle ensures the encapsulation of all and only the functions that access the internal representation of a type. C++ supports this principle via member functions and friend functions, but it does not enforce it. Programmers can declare parts or all of

3690-447: The class's members with constant expressions. The destructors for such types must be trivial. The copy constructor for a type with any constexpr constructors should usually also be defined as a constexpr constructor, to allow objects of the type to be returned by value from a constexpr function. Any member function of a class, such as copy constructors, operator overloads, etc., can be declared as constexpr , so long as they meet

3772-420: The class. This can hide the details of data implementation, allowing the designer to later fundamentally change the implementation without changing the interface in any way. Inheritance allows one data type to acquire properties of other data types. Inheritance from a base class may be declared as public, protected, or private. This access specifier determines whether unrelated and derived classes can access

3854-454: The compilation fails because the immediate function invoked function which is not usable in constant expressions. In other words, the compilation stops after failed assertion. The typical compilation error message would display: Here's another example of using immediate functions as constructors which enables compile-time argument checking: The compilation fails here with the message: Here's an example of compile time function evaluation in

3936-413: The compiler to instantiate a template: C++11 now provides this syntax: which tells the compiler not to instantiate the template in this translation unit. These features exist for the primary purpose of making the language easier to use. These can improve type safety, minimize code repetition, make erroneous code less likely, etc. C++03 inherited the initializer-list feature from C. A struct or array

4018-466: The compiler to understand, and verify, that get_five() is a compile-time constant. Using constexpr on a function imposes some limits on what that function can do. First, the function must have a non-void return type. Second, the function body cannot declare variables or define new types. Third, the body may contain only declarations, null statements and a single return statement. There must exist argument values such that, after argument substitution,

4100-503: The core language and the standard library. In 2014, C++14 (also known as C++1y) was released as a small extension to C++11, featuring mainly bug fixes and small improvements. The Draft International Standard ballot procedures completed in mid-August 2014. After C++14, a major revision C++17 , informally known as C++1z, was completed by the ISO C++ committee in mid July 2017 and was approved and published in December 2017. As part of

4182-436: The correct constructor for those particular arguments. This is seen in the emplace_back set of the C++ standard library methods. C++ has always had the concept of constant expressions. These are expressions such as 3+4 that will always yield the same results, at compile time and at runtime. Constant expressions are optimization opportunities for compilers, and compilers frequently execute them at compile time and hardcode

SECTION 50

#1732772650104

4264-511: The expression in the return statement produces a constant expression. Before C++11, the values of variables could be used in constant expressions only if the variables are declared const, have an initializer which is a constant expression, and are of integral or enumeration type. C++11 removes the restriction that the variables must be of integral or enumeration type if they are defined with the constexpr keyword: Such data variables are implicitly const, and must have an initializer which must be

4346-634: The factorial would be similar to what one would write for run-time evaluation e.g. using C++11 constexpr. In C++11 , this technique is known as generalized constant expressions ( constexpr ). C++14 relaxes the constraints on constexpr – allowing local declarations and use of conditionals and loops (the general restriction that all data required for the execution be available at compile-time remains). Here's an example of compile time function evaluation in C++14: In C++20 , immediate functions were introduced, and compile-time function execution

4428-617: The four features commonly present in OOP (and some non-OOP) languages: abstraction , encapsulation , inheritance , and polymorphism . One distinguishing feature of C++ classes compared to classes in other programming languages is support for deterministic destructors , which in turn provide support for the Resource Acquisition is Initialization (RAII) concept. Encapsulation is the hiding of information to ensure that data structures and operators are used as intended and to make

4510-426: The inherited public and protected members of the base class. Only public inheritance corresponds to what is usually meant by "inheritance". The other two forms are much less frequently used. If the access specifier is omitted, a "class" inherits privately, while a "struct" inherits publicly. Base classes may be declared as virtual; this is called virtual inheritance . Virtual inheritance ensures that only one instance of

4592-435: The initial standardization in 1998, C++ was developed by Stroustrup at Bell Labs since 1979 as an extension of the C language; he wanted an efficient and flexible language similar to C that also provided high-level features for program organization. Since 2012, C++ has been on a three-year release schedule with C++26 as the next planned standard. Despite its widespread adoption, some notable programmers have criticized

4674-410: The language features described above. Compile-time polymorphism does not allow for certain run-time decisions, while runtime polymorphism typically incurs a performance penalty. Compile-time function execution In computing , compile-time function execution (or compile time function evaluation , or general constant expressions ) is the ability of a compiler , that would normally compile

4756-399: The language had been referred to as "new C" and "C with Classes" before acquiring its final name. Throughout C++'s life, its development and evolution has been guided by a set of principles: C++ is standardized by an ISO working group known as JTC1/SC22/WG21 . So far, it has published seven revisions of the C++ standard and is currently working on the next revision, C++26 . In 1998,

4838-516: The language was too slow for practical use, while BCPL was fast but too low-level to be suitable for large software development. When Stroustrup started working in AT&;T Bell Labs , he had the problem of analyzing the UNIX kernel with respect to distributed computing . Remembering his PhD experience, Stroustrup set out to enhance the C language with Simula -like features. C was chosen because it

4920-502: The nature of the wording of rvalue references, and to some modification to the wording for lvalue references (regular references), rvalue references allow developers to provide perfect function forwarding. When combined with variadic templates , this ability allows for function templates that can perfectly forward arguments to another function that takes those particular arguments. This is most useful for forwarding constructor parameters, to create factory functions that will automatically call

5002-465: The new std::vector<T> , then set the pointer inside the rvalue to null. Since the temporary will never again be used, no code will try to access the null pointer, and because the pointer is null, its memory is not deleted when it goes out of scope. Hence, the operation not only forgoes the expense of a deep copy, but is safe and invisible. Rvalue references can provide performance benefits to existing code without needing to make any changes outside

SECTION 60

#1732772650104

5084-821: The number of base classes to one (interfaces, unlike classes, provide only declarations of member functions, no implementation or member data). An interface as in C# and Java can be defined in C++ as a class containing only pure virtual functions, often known as an abstract base class or "ABC". The member functions of such an abstract base class are normally explicitly defined in the derived class, not inherited implicitly. C++ virtual inheritance exhibits an ambiguity resolution feature called dominance . C++ provides more than 35 operators, covering basic arithmetic, bit manipulation, indirection, comparisons, logical operations and others. Almost all operators can be overloaded for user-defined types, with

5166-556: The operator, nor does it change the number of operands that the operator uses (any operand may however be ignored by the operator, though it will be evaluated prior to execution). Overloaded " && " and " || " operators lose their short-circuit evaluation property. Polymorphism enables one common interface for many implementations, and for objects to act differently under different circumstances. C++ supports several kinds of static (resolved at compile-time ) and dynamic (resolved at run-time ) polymorphisms , supported by

5248-427: The order of initialization between compilation units. Variables of this type are very similar to static storage duration objects. The main difference is the creation time is just before thread creation, and destruction is done after the thread has been joined. The most common variable types in C++ are local variables inside a function or block, and temporary variables. The common feature about automatic variables

5330-452: The parent object is created. Array members are initialized from 0 to the last member of the array in order. Member variables are destroyed when the parent object is destroyed in the reverse order of creation. i.e. If the parent is an "automatic object" then it will be destroyed when it goes out of scope which triggers the destruction of all its members. Temporary variables are created as the result of expression evaluation and are destroyed when

5412-438: The purpose of allowing "move semantics". A chronic performance problem with C++03 is the costly and unneeded deep copies that can happen implicitly when objects are passed by value. To illustrate the issue, consider that an std::vector<T> is, internally, a wrapper around a C-style array with a defined size. If an std::vector<T> temporary is created or returned from a function, it can be stored only by creating

5494-430: The representation of a type to be public, and they are allowed to make public entities not part of the representation of a type. Therefore, C++ supports not just object-oriented programming, but other decomposition paradigms such as modular programming . It is generally considered good practice to make all data private or protected, and to make public only those functions that are part of a minimal interface for users of

5576-440: The requirements for constexpr functions. This allows the compiler to copy objects at compile time, perform operations on them, etc. If a constexpr function or constructor is called with arguments which aren't constant expressions, the call behaves as if the function were not constexpr, and the resulting value is not a constant expression. Likewise, if the expression in the return statement of a constexpr function does not evaluate to

5658-425: The results in the program. Also, in several places, the C++ specification requires using constant expressions. Defining an array requires a constant expression, and enumerator values must be constant expressions. However, a constant expression has never been allowed to contain a function call or object constructor. So a piece of code as simple as this is invalid: This was not valid in C++03, because get_five() + 7

5740-487: The right side of an assignment) were intended to never be modifiable — just as in C — and were considered to be indistinguishable from const T& types; nevertheless, in some cases, temporaries could have been modified, a behavior that was even considered to be a useful loophole. C++11 adds a new non-const reference type called an rvalue reference , identified by T&& . This refers to temporaries that are permitted to be modified after they are initialized, for

5822-501: The specific arguments used to instantiate it. After instantiation, the resulting code is equivalent to code written specifically for the passed arguments. In this manner, templates provide a way to decouple generic, broadly applicable aspects of functions and classes (encoded in templates) from specific aspects (encoded in template parameters) without sacrificing performance due to abstraction. C++ introduces object-oriented programming (OOP) features to C. It offers classes , which provide

5904-572: The standard library further, and providing more facilities to C++ programmers. After a minor C++14 update released in December 2014, various new additions were introduced in C++17 . After becoming finalized in February 2020, a draft of the C++20 standard was approved on 4 September 2020, and officially published on 15 December 2020. On January 3, 2018, Stroustrup was announced as the 2018 winner of

5986-409: The standard library. The type of the returned value of a function returning an std::vector<T> temporary does not need to be changed explicitly to std::vector<T> && to invoke the move constructor, as temporaries are considered rvalues automatically. (However, if std::vector<T> is a C++03 version without a move constructor, then the copy constructor will be invoked with

6068-579: The standardization process, ISO also publishes technical reports and specifications : More technical specifications are in development and pending approval, including new set of concurrency extensions. The C++ language has two main components: a direct mapping of hardware features provided primarily by the C subset, and zero-overhead abstractions based on those mappings. Stroustrup describes C++ as "a light-weight abstraction programming language [designed] for building and using efficient and elegant abstractions"; and "offering both hardware access and abstraction

6150-409: The statement containing the expression has been fully evaluated (usually at the ; at the end of a statement). These objects have a dynamic lifespan and can be created directly with a call to new and destroyed explicitly with a call to delete . C++ also supports malloc and free , from C, but these are not compatible with new and delete . Use of new returns an address to

6232-483: The static initialization phase can be completed at compile time and saved in the data partition of the executable. Dynamic initialization involves all object initialization done via a constructor or function call (unless the function is marked with constexpr , in C++11). The dynamic initialization order is defined as the order of declaration within the compilation unit (i.e. the same file). No guarantees are provided about

6314-529: The type like std::initializer_list<SomeType>{args} (and so on for other varieties of construction syntax). The list can be copied once constructed, which is cheap and will act as a copy-by-reference (the class is typically implemented as a pair of begin/end pointers). An std::initializer_list is constant: its members cannot be changed once it is created, and nor can the data in those members be changed (which rules out moving from them, requiring copies into class members, etc.). Although its construction

6396-470: The usage model more obvious to the developer. C++ provides the ability to define classes and functions as its primary encapsulation mechanisms. Within a class, members can be declared as either public, protected, or private to explicitly enforce encapsulation. A public member of the class is accessible to any function. A private member is accessible only to functions that are members of that class and to functions and classes explicitly granted access permission by

6478-407: Was general-purpose, fast, portable, and widely used. In addition to C and Simula's influences, other languages influenced this new language, including ALGOL 68 , Ada , CLU , and ML . Initially, Stroustrup's "C with Classes" added features to the C compiler, Cpre, including classes , derived classes , strong typing , inlining , and default arguments . In 1982, Stroustrup started to develop

6560-496: Was made more accessible and flexible with relaxed constexpr restrictions. Since function Factorial is marked consteval , it is guaranteed to invoke at compile-time without being forced in another manifestly constant-evaluated context. Hence, the usage of immediate functions offers wide uses in metaprogramming, and compile-time checking (used in C++20 text formatting library). Here's an example of using immediate functions in compile-time function execution: In this example,

6642-424: Was not yet an official standard. The first commercial implementation of C++ was released in October of the same year. In 1989, C++ 2.0 was released, followed by the updated second edition of The C++ Programming Language in 1991. New features in 2.0 included multiple inheritance, abstract classes, static member functions, const member functions , and protected members. In 1990, The Annotated C++ Reference Manual

6724-472: Was published. This work became the basis for the future standard. Later feature additions included templates , exceptions , namespaces , new casts , and a Boolean type . In 1998, C++98 was released, standardizing the language, and a minor update ( C++03 ) was released in 2003. After C++98, C++ evolved relatively slowly until, in 2011, the C++11 standard was released, adding numerous new features, enlarging

#103896