C++

From wiki1463
Jump to navigation Jump to search

Template:Short description Template:Pp Template:Pp-move-vandalism Template:Redirect-multi Template:Use dmy dates Template:Infobox programming language Template:C++ sidebar C++Template:Efn 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, adding object-oriented (OOP) functionality and later expanding significantly with the addition of functional programming features. It supports low-level memory manipulation for systems like microcomputers and has been used for the creation and maintenance of Microsoft Windows. C++ has also added support for generic programming through the use of templates. It 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.[1]

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.[2] C++ has also been found useful in many other contexts, with key strengths being software infrastructure and resource-constrained applications,[2] 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).[3]

C++ 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).[4] 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 next Template:Nowrap standard superseded these with new features and an enlarged standard library. Before 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.[5] Since 2012, C++ has been on a three-year release schedule[6] with C++29 as the next planned standard.[7]

History

File:BjarneStroustrup.jpg
Bjarne Stroustrup, the creator of C++, in his AT&T New Jersey office, Template:Circa

In 1979, Bjarne Stroustrup, a Danish computer scientist, began work on "C with Classes", the predecessor to C++.[8] Initially, Stroustrup's "C with Classes" added features to the C compiler, Cpre, including classes, derived classes, strong typing, inlining, and default arguments.[9]

File:20160121 CppFRUG Joel Falcou CppQuiz 3.jpg
A quiz on C++11 features being given in Paris in 2015

In 1982, Stroustrup began designing a cleaned-up and extended successor to C with Classes, using traditional compiler technology.[10] The language was briefly called C84 before the name C++ was adopted; the name was suggested by Rick Mascitti and refers to C's ++ increment operator.[10][11] The resulting language included virtual functions, function and operator overloading, references, const, improved type checking, user-controlled free-store memory allocation (new/delete), and BCPL-style single-line // comments.[10] Stroustrup also designed and implemented Cfront, the C++ compiler front end, between spring 1982 and summer 1983.[10]

In 1985, the first edition of The C++ Programming Language was released, which became the definitive reference for the language, as there was not yet an official standard.[12] The first commercial implementation of C++ was released in October of the same year.[8]

In 1989, C++ 2.0 was released, followed by the updated second edition of The C++ Programming Language in 1991.[13] 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 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 the standard library further, and providing more facilities to C++ programmers. After a minor Template:Nowrap update released in December 2014, various new additions were introduced in C++17.[14] After becoming finalized in February 2020,[15] a draft of the C++20 standard was approved on 4 September 2020, and officially published on 15 December 2020.[16][17]

On January 3, 2018, Stroustrup was announced as the 2018 winner of the Charles Stark Draper Prize for Engineering, "for conceptualizing and developing the C++ programming language".[18]

In December 2022, C++ ranked third on the TIOBE index, surpassing Java for the first time in the history of the index. Template:As of, the language ranks second after Python, with Java being in third.[19]

In March 2025, Stroustrup issued a call for the language community to defend it. Since the language allows manual memory management, bugs that represent security risks such as buffer overflow may be introduced in programs when inadvertently misused by the programmer.[20]

Etymology

According to Stroustrup, "the name signifies the evolutionary nature of the changes from C."[21] This name is credited to Rick Mascitti (mid-1983)[9] 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, the language had been referred to as "new C" and "C with Classes"[9][22] before acquiring its final name.

Philosophy

Throughout C++'s life, its development and evolution has been guided by a set of principles:[23]

  • It must be driven by actual problems and its features should be immediately useful in real world programs.
  • Every feature should be implementable (with a reasonably obvious way to do so).
  • Programmers should be free to pick their own programming style, and that style should be fully supported by C++.
  • Allowing a useful feature is more important than preventing every possible misuse of C++.
  • It should provide facilities for organizing programs into separate, well-defined parts, and provide facilities for combining separately developed parts.
  • No implicit violations of the type system (but allow explicit violations; that is, those explicitly requested by the programmer).
  • User-created types need to have the same support and performance as built-in types.
  • Unused features should not negatively impact created executables (e.g. in lower performance).
  • There should be no language beneath C++ (except assembly language).
  • C++ should work alongside other existing programming languages, rather than fostering its own separate and incompatible programming environment.
  • If the programmer's intent is unknown, allow the programmer to specify it by providing manual control.

Standardization

Template:Main

C++ standards
Year ISO/IEC Standard Informal name
1998 14882:1998[24] C++98
2003 14882:2003[25] C++03
2011 14882:2011[26] C++11, C++0x
2014 14882:2014[27] C++14, C++1y
2017 14882:2017[28] C++17, C++1z
2020 14882:2020[29] C++20, C++2a
2024 14882:2024[4] C++23, C++2b
scope="row" Template:TBA C++26, C++2c

C++ is standardized by an ISO working group known as JTC1/SC22/WG21. The working group holds three week-long meetings each year.[30] So far, it has published seven revisions of the C++ standard and is currently working on the next revision, C++26.

File:C++ Standards Committee meeting - July 1996 Stockholm - Wednesday general session.jpg
Scene during the C++ standards committee meeting in Stockholm in 1996

In 1998, 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.[31] C++11 (14882:2011) included many additions to both the core language and the standard library.[26]

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.[32] The Draft International Standard ballot procedures completed in mid-August 2014.[33]

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.[34]

As part of the standardization process, ISO also publishes technical reports and specifications:

  • ISO/IEC TR 18015:2006[35] on the use of C++ in embedded systems and on performance implications of C++ language and library features,
  • ISO/IEC TR 19768:2007[36] (also known as the C++ Technical Report 1) on library extensions mostly integrated into C++11,
  • ISO/IEC TR 29124:2010[37] on special mathematical functions, integrated into Template:Nowrap,
  • ISO/IEC TR 24733:2011[38] on decimal floating-point arithmetic,
  • ISO/IEC TS 18822:2015[39] on the standard filesystem library, integrated into C++17,
  • ISO/IEC TS 19570:2015[40] on parallel versions of the standard library algorithms, integrated into C++17,
  • ISO/IEC TS 19841:2015[41] on software transactional memory,
  • ISO/IEC TS 19568:2015[42] on a new set of library extensions, some of which are already integrated into C++17,
  • ISO/IEC TS 19217:2015[43] on the C++ concepts, integrated into C++20,
  • ISO/IEC TS 19571:2016[44] on the library extensions for concurrency, some of which are already integrated into C++20,
  • ISO/IEC TS 19568:2017[45] on a new set of general-purpose library extensions,
  • ISO/IEC TS 21425:2017[46] on the library extensions for ranges, integrated into C++20,
  • ISO/IEC TS 22277:2017[47] on coroutines, integrated into C++20,
  • ISO/IEC TS 19216:2018[48] on the networking library,
  • ISO/IEC TS 21544:2018[49] on modules, integrated into C++20,
  • ISO/IEC TS 19570:2018[50] on a new set of library extensions for parallelism
  • ISO/IEC TS 23619:2021[51] on new extensions for reflective programming (reflection),
  • ISO/IEC TS 9922:2024[52] on new set of concurrency extensions, and
  • ISO/IEC TS 19568:2024[53] on another new set of library extensions.

More technical specifications are in development and pending approval.

Language

Template:Main 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";[2] and "offering both hardware access and abstraction is the basis of C++. Doing it efficiently is what distinguishes it from other languages."[54]

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 adapted from Bjarne Stroustrup's version of the Hello world program that uses the C++ Standard Library stream facility to write a message to standard output:[55][56]Template:Efn

#include <iostream>

int main() {
    std::cout << "Hello, world!\n";
}

Since C++23, with the introduction of [[Input/output (C++)|Template:Mono]] functions and [[Modules (C++)#Standard library modules|module Template:Mono]], this can be expressed less verbosely as:

import std;

int main() {
    std::println("Hello, world!");
}

Standard library

File:ANSI ISO C++ WP.jpg
The draft "Working Paper" standard that became approved as C++98; half of its size was devoted to the C++ Standard Library.

Template:Main

The C++ standard specifies both the core language and the standard library. The library clauses describe the contents of the library, how a well-formed C++ program uses it, and the requirements placed on conforming implementations.[57]

The standard library is organized into areas such as language support, diagnostics, memory management, metaprogramming, general utilities, containers, iterators, ranges, algorithms, strings, text processing, numerics, time, input/output, and concurrency support.[57] It also provides the facilities of the C standard library through C++ headers such as Template:Code and Template:Code; except for names defined as macros in C, these declarations are placed in namespace Template:Code.[58]

Library facilities are traditionally made available by including standard headers, for example #include <vector>. Since C++20, importable standard library headers may also be imported as header units. C++23 added the named standard library modules std and Template:Code: Template:Code exports declarations in namespace Template:Code, while Template:Code also exports corresponding global namespace declarations for C library facilities.[59]

A major influence on the standard library was the Standard Template Library (STL), a generic programming library proposed to the C++ standards committee by Alexander Stepanov and Meng Lee in 1994. Its model of containers, iterators, and generic algorithms became part of the Standard C++ Library, although the standard library also includes many other facilities, such as input/output, localization, diagnostics, and the C library subset.[60][61]

C++ Core Guidelines

The C++ Core Guidelines[62] are an initiative led by Stroustrup and Herb Sutter, the convener and chair of the C++ ISO Working Group, to help programmers write 'Modern C++' by using best practices for the language standards C++11 and newer, and to help developers of compilers and static checking tools to create rules for catching bad programming practices. The main aim is to efficiently and consistently write type and resource safe C++. Despite this, the guidelines are not endorsed by the ISO C++ standards committee and do not represent the consensus of the committee.[63]

The Core Guidelines were announced[64] in the opening keynote at CPPCon 2015.

The Guidelines are accompanied by the Guideline Support Library (GSL), a header-only library of types and functions to implement the Core Guidelines and static checker tools for enforcing Guideline rules.[65] One of the largest implementations of this is Microsoft's implementation.[66] Many of the existing features in the GSL have been later integrated into the language, such as std::byte, smart pointers, and std::span.[66]

Compatibility

To give compiler vendors greater freedom, the C++ standards committee decided not to dictate the implementation of name mangling, exception handling, and other implementation-specific features. The downside of this decision is that object code produced by different compilers is expected to be incompatible. There are, however, attempts to standardize compilers for particular machines or operating systems. For example, the Itanium C++ ABI is processor-independent (despite its name) and is implemented by GCC and Clang.[67]

With C

Template:Main C++ is often considered to be a superset of C but this is not strictly true.[68] Most C code can easily be made to compile correctly in C++ but there are a few differences that cause some valid C code to be invalid or behave differently in C++. For example, C allows implicit conversion from void* to other pointer types but C++ does not (for type safety reasons). Also, C++ defines many new keywords, such as new and class, which may be used as identifiers (for example, variable names) in a C program.

Some incompatibilities have been removed by the 1999 revision of the C standard (C99), which now supports C++ features such as line comments (//) and declarations mixed with code. On the other hand, C99 introduced a number of new features that C++ did not support that were incompatible or redundant in C++, such as variable-length arrays, native complex-number types (however, the std::complex class in the C++ standard library provides similar functionality, although not code-compatible), designated initializers, compound literals, and the restrict keyword.[69] Some of the C99-introduced features were included in the subsequent version of the C++ standard, C++11 (out of those which were not redundant).[70][71][72] However, the C++11 standard introduces new incompatibilities, such as disallowing assignment of a string literal to a character pointer, which remains valid C.

To intermix C and C++ code, any function declaration or definition that is to be called from/used both in C and C++ must be declared with C linkage by placing it within an extern "C" {/*...*/} block. Such a function may not rely on features depending on name mangling (i.e., function overloading).

Inline assembly

Programs developed in C or C++ often use inline assembly to take advantage of its low-level functionalities, greater speed, and enhanced control compared to high-level programming languages[73][74] when optimizing for performance is essential. C++ provides support for embedding assembly language using asm declarations,[75] but the compatibility of inline assembly varies significantly between compilers and architectures. Unlike high-level language features such as Python or Java, assembly code is highly dependent on the underlying processor and compiler implementation.

Variations across compilers

Different C++ compilers implement inline assembly in distinct ways.

  • GCC (GNU Compiler Collection) and Clang:[76] Both compilers use the GCC extended inline assembly syntax, using the __asm__ keyword instead of asm when writing code that can be compiled with -ansi and -std options, which allows specifying input/output operands and clobbered registers. This approach is also adopted elsewhere, including by Intel[77] and IBM[78] compilers.
  • MSVC (Microsoft Visual C++): The inline assembler is built into the compiler. Previously supported inline assembly via the __asm keyword, but this support has been removed in 64-bit mode, requiring separate .asm modules instead.[79]
  • TI ARM Clang and Embedded Compilers:[80] Some embedded system compilers, like Texas Instruments' TI Arm Clang, allow inline assembly but impose stricter rules to avoid conflicts with register conventions and calling conventions.

Interoperability between C++ and Assembly

C++ provides two primary methods of integrating ASM code: standalone assembly files, where assembly code is written separately and linked with C++ code.[81] And inline assembly, where assembly code is embedded within C++ code using compiler-specific extensions.

See also

Template:Portal Template:Wikibooks

Notes

Template:Notelist Template:Clear

References

Template:Reflist

Further reading

Template:Refbegin

Template:Refend

Template:Sister bar Template:C++ programming language Template:Programming languages Template:ISO standards Template:List of International Electrotechnical Commission standards Template:Authority control

  1. ↑ Template:Cite book
  2. ↑ 2.0 2.1 2.2 Template:Cite web
  3. ↑ Template:Cite web
  4. ↑ 4.0 4.1 Template:Cite web
  5. ↑ Template:Cite web
  6. ↑ Template:Cite web
  7. ↑ Template:Cite web
  8. ↑ 8.0 8.1 Template:Cite web
  9. ↑ 9.0 9.1 9.2 Template:Cite web
  10. ↑ 10.0 10.1 10.2 10.3 Template:Cite conference
  11. ↑ Template:Cite magazine
  12. ↑ Template:Cite web
  13. ↑ Template:Cite web
  14. ↑ Template:Cite web
  15. ↑ Template:Cite web
  16. ↑ Template:Cite web
  17. ↑ Template:Cite web
  18. ↑ Template:Cite press release
  19. ↑ Template:Cite web
  20. ↑ Template:Cite news
  21. ↑ Template:Cite web
  22. ↑ Template:Cite web
  23. ↑ Template:Cite web
  24. ↑ Template:Cite web
  25. ↑ Template:Cite web
  26. ↑ 26.0 26.1 Template:Cite web
  27. ↑ Template:Cite web
  28. ↑ Template:Cite web
  29. ↑ Template:Cite web
  30. ↑ Template:Cite web
  31. ↑ Template:Cite web
  32. ↑ Template:Cite web
  33. ↑ Template:Cite web
  34. ↑ Template:Cite web
  35. ↑ Template:Cite web
  36. ↑ Template:Cite web
  37. ↑ Template:Cite web
  38. ↑ Template:Cite web
  39. ↑ Template:Cite web
  40. ↑ Template:Cite web
  41. ↑ Template:Cite web
  42. ↑ Template:Cite web
  43. ↑ Template:Cite web
  44. ↑ Template:Cite web
  45. ↑ Template:Cite web
  46. ↑ Template:Cite web
  47. ↑ Template:Cite web
  48. ↑ Template:Cite web
  49. ↑ Template:Cite web
  50. ↑ Template:Cite web
  51. ↑ Template:Cite web
  52. ↑ Template:Cite web
  53. ↑ Template:Cite web
  54. ↑ Template:Cite web
  55. ↑ Template:Cite book
  56. ↑ Template:Cite web
  57. ↑ 57.0 57.1 Template:Cite web
  58. ↑ Template:Cite web
  59. ↑ Template:Cite web
  60. ↑ Template:Cite magazine
  61. ↑ Template:Cite web
  62. ↑ Template:Cite web
  63. ↑ Template:Cite web
  64. ↑ Template:Cite web
  65. ↑ Template:Cite web
  66. ↑ 66.0 66.1 Template:Cite web
  67. ↑ Template:Cite web
  68. ↑ Template:Cite web
  69. ↑ Template:Cite web
  70. ↑ Template:Cite web
  71. ↑ Template:Cite web
  72. ↑ Template:Cite web
  73. ↑ Bokil, Milind A. (2021). "Writing Assembly Routines within C/C++ and Java Programs". ResearchGate. Retrieved 1 April 2025.
  74. ↑ Template:Cite journal
  75. ↑ cppreference.com contributors. "asm declaration". cppreference.com. Retrieved 1 April 2025.
  76. ↑ Template:Cite web
  77. ↑ Intel Corporation. "Inline Assembly". Intel® C++ Compiler Classic Developer Guide and Reference, Version 2021.9. Retrieved 1 April 2025.
  78. ↑ IBM. "Inline assembly statements (IBM extension)". IBM Documentation. Retrieved 1 April 2025.
  79. ↑ Template:Cite web
  80. ↑ Template:Cite web
  81. ↑ Template:Cite web