C++

{{Short description|General-purpose programming language}}

{{pp-move-vandalism|small=yes}}

{{Redirect-multi|2|cxx|.cxx|other uses|CXX (disambiguation){{!}}CXX}}

{{Use dmy dates|date=January 2020}}

{{Infobox programming language

| name = C++

| logo = ISO C++ Logo.svg

| logo size = 125px

| logo caption = Logo endorsed by the C++ standards committee

| paradigms = Multi-paradigm: procedural, imperative, functional, object-oriented, generic, modular

| family = C

| designer = Bjarne Stroustrup

| developer = ISO/IEC JTC 1 (Joint Technical Committee 1) / SC 22 (Subcommittee 22) / WG 21 (Working Group 21)

| released = {{Start date and age|df=yes|1985}}

| latest release version = C++23 (ISO/IEC 14882:2024)

| latest release date = {{Start date and age|2024|10|19|df=yes}}

| latest preview version = C++26

| latest preview date = {{Start date and age|2024|10|16|df=yes}}

| typing = Static, strong, nominative, partially inferred

| scope =

| platform =

| operating system = Cross-platform

| file ext = .C, .cc, .cpp, .cxx, {{nowrap|.c++}}, .h, .H, .hh, .hpp, .hxx, {{nowrap|.h++}} .cppm, .ixx{{cite web|url=https://learn.microsoft.com/en-us/cpp/cpp/modules-cpp?view=msvc-170|title=Overview of modules in C++|date=24 April 2023 |publisher=Microsoft}}

| file format =

| implementations = {{nowraplinks| GCC, LLVM Clang, Microsoft Visual C++, Embarcadero C++Builder, Intel C++ Compiler, IBM XL C++, EDG}}

| dialects =

| wikibooks = C++ Programming

| influenced by = Ada, ALGOL 68,{{cite book |last=Stroustrup |first=Bjarne |date=1996 |pages=699–769 |chapter=A history of C++: 1979-1991 |title=History of programming languages---II |publisher=ACM |doi=10.1145/234286.1057836 |doi-access=free }} BCPL,{{cite web |url=https://www.youtube.com/watch?v=15QF2q66NhU |title=C++20: Reaching for the Aims of C++ - Bjarne Stroustrup - CppCon 2021 |first=Bjarne |last=Stroustrup |publisher=CppCon |date=December 16, 2021 |access-date=30 December 2021 |archive-date=30 December 2021 |archive-url=https://web.archive.org/web/20211230092718/https://www.youtube.com/watch?v=15QF2q66NhU |url-status=live }} C, CLU, F#,{{cite journal | last=Stroustrup | first=Bjarne | title=Thriving in a crowded and changing world: C++ 2006–2020 | journal=Proceedings of the ACM on Programming Languages | publisher=Association for Computing Machinery (ACM) | volume=4 | issue=HOPL | date=2020-06-12 | issn=2475-1421 | doi=10.1145/3386320 | pages=1–168| s2cid=219603741 | doi-access=free }}For the idea of the C++20 stackless coroutines. ML, Mesa, Modula-2, Simula, Smalltalk

| influenced = Ada 95, C#,{{cite journal |last=Naugler |first=David |date=May 2007 |title=C# 2.0 for C++ and Java programmer: conference workshop |journal=Journal of Computing Sciences in Colleges |volume=22 |issue=5 |quote=Although C# has been strongly influenced by Java it has also been strongly influenced by C++ and is best viewed as a descendant of both C++ and Java.}} C99, Carbon, Chapel,{{cite web|title=Chapel spec (Acknowledgements)|url=https://chapel-lang.org/spec/spec-0.98.pdf|date=1 October 2015|access-date=14 January 2016|publisher=Cray Inc|archive-date=24 June 2018|archive-url=https://web.archive.org/web/20180624150422/https://chapel-lang.org/spec/spec-0.98.pdf|url-status=live}} Clojure,{{cite web |url=https://www.codequarterly.com/2011/rich-hickey/ |archive-url=https://web.archive.org/web/20170111184835/http://www.codequarterly.com/2011/rich-hickey/ |url-status=dead |archive-date=2017-01-11 |title=Rich Hickey Q&A |first1=Michael |last1=Fogus |website=Code Quarterly |access-date=2017-01-11}} D, Java,{{cite web |url=https://books.google.com/books?id=0rUtBAAAQBAJ&pg=PA133 |title=Cracking The Java Programming Interview :: 2000+ Java Interview Que/Ans |author=Harry. H. Chaudhary |access-date=29 May 2016 |date=28 July 2014 |archive-date=27 May 2021 |archive-url=https://web.archive.org/web/20210527025512/https://books.google.com/books?id=0rUtBAAAQBAJ&pg=PA133 |url-status=live }} JS++,{{cite web|url=https://www.onux.com/jspp/blog/scaling-jspp-abstraction-performance-and-readability/|title=Scaling JS++: Abstraction, Performance, and Readability|author=Roger Poon|date=1 May 2017|access-date=21 April 2020|archive-date=11 May 2020|archive-url=https://web.archive.org/web/20200511095442/https://www.onux.com/jspp/blog/scaling-jspp-abstraction-performance-and-readability/|url-status=live}} Lua,{{Cite web|url=https://www.lua.org/history.html|title=The evolution of an extension language: a history of Lua|website=www.lua.org|access-date=2023-01-04}} Nim,{{cite web|url=https://nim-lang.org/faq.html|title=FAQ Nim Programming Language|access-date=2020-04-21|archive-date=11 July 2017|archive-url=https://web.archive.org/web/20170711004631/https://nim-lang.org/faq.html|url-status=live}} Objective-C++, Perl, PHP, Python,{{Cite web|url=https://docs.python.org/tutorial/classes.html|title=9. Classes — Python 3.6.4 documentation|website=docs.python.org|access-date=2018-01-09|archive-date=23 October 2012|archive-url=https://web.archive.org/web/20121023030209/http://docs.python.org/tutorial/classes.html|url-status=live}} Rust,{{Cite web|url=https://doc.rust-lang.org/reference/influences.html|title=Influences - The Rust Reference|website=doc.rust-lang.org|access-date=2023-01-04}} Seed7

}}

C++ ({{IPAc-en|'|s|i:|_|p|l|V|s|_|p|l|V|s}}, pronounced "C plus plus" and sometimes abbreviated as CPP or CXX) 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) features, it has since expanded significantly over time adding more OOP and other features; {{as of|1997|lc=true}}/C++98 standardization, C++ has added functional features, in addition to facilities for low-level memory manipulation for systems like microcomputers or to make operating systems like Linux or Windows, and even later came features like generic (template) programming. C++ 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.{{Cite book |last=Stroustrup |first=Bjarne |author-link=Bjarne Stroustrup |title=The C++ Programming Language |year=1997 |edition=Third |chapter=1 |publisher=Addison-Wesley |isbn=0-201-88954-4 |oclc=59193992 |url=https://archive.org/details/cprogramminglang00stro_0 }}

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.{{cite web|url=https://www.youtube.com/watch?v=86xWVb4XIyE|author=Stroustrup, B.|title=Lecture:The essence of C++. University of Edinburgh.|website=YouTube|date=6 May 2014|access-date=12 June 2015|archive-date=28 April 2015|archive-url=https://web.archive.org/web/20150428003608/https://www.youtube.com/watch?v=86xWVb4XIyE|url-status=live}} 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).{{cite web |url=http://www.stroustrup.com/applications.html |title=C++ Applications |date=17 February 2014 |access-date=5 May 2014 |first=Bjarne |last=Stroustrup |website=stroustrup.com |archive-date=4 April 2021 |archive-url=https://web.archive.org/web/20210404065717/https://www.stroustrup.com/applications.html |url-status=live }}

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). 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 {{nowrap|C++23}} standard supersedes 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.{{cite web |title=Bjarne Stroustrup's Homepage |url=http://www.stroustrup.com |website=www.stroustrup.com |access-date=15 May 2013 |archive-date=14 May 2019 |archive-url=https://web.archive.org/web/20190514123147/http://www.stroustrup.com/ |url-status=live }} Since 2012, C++ has been on a three-year release schedule{{Cite web|url=http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2020/p1000r4.pdf|title=C++ IS schedule|access-date=9 August 2020|archive-date=10 August 2020|archive-url=https://web.archive.org/web/20200810105609/http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2020/p1000r4.pdf|url-status=live}} with C++26 as the next planned standard.{{cite web |title = C++; Where it's heading |url = https://dzone.com/articles/c-where-is-it-heading-and-what-are-the-new-feature |access-date = 3 December 2018 |archive-date = 3 December 2018 |archive-url = https://web.archive.org/web/20181203104022/https://dzone.com/articles/c-where-is-it-heading-and-what-are-the-new-feature |url-status = live }}

Despite its widespread adoption, some notable programmers have criticized the C++ language, including Linus Torvalds,{{cite mailing list |title=Re: [RFC] Convert builin-mailinfo.c to use The Better String Library |date=6 September 2007 |url=https://lwn.net/Articles/249460/ |access-date=31 March 2015 |archive-url=https://web.archive.org/web/20210308222938/https://lwn.net/Articles/249460/ |archive-date=8 March 2021 |url-status=live}} Richard Stallman,{{cite mailing list |title=Re: Efforts to attract more users? |date=12 July 2010 |url=http://harmful.cat-v.org/software/c++/rms |access-date=31 March 2015 |archive-url=https://web.archive.org/web/20150321233514/http://harmful.cat-v.org/software/c%20%20/rms |archive-date=21 March 2015 |url-status=live}} Joshua Bloch, Ken Thompson,{{cite web |author=Andrew Binstock |date=18 May 2011 |title=Dr. Dobb's: Interview with Ken Thompson |work=Dr. Dobb's |url=https://www.drdobbs.com/open-source/interview-with-ken-thompson/229502480 |url-status=live |archive-url=https://web.archive.org/web/20140313072938/http://www.drdobbs.com/open-source/interview-with-ken-thompson/229502480 |archive-date=13 March 2014 |access-date=7 February 2014}}{{cite book |author=Peter Seibel |url=https://books.google.com/books?id=nneBa6-mWfgC&pg=PA475 |title=Coders at Work: Reflections on the Craft of Programming |date=16 September 2009 |publisher=Apress |isbn=978-1-4302-1948-4 |pages=475–476 |access-date=9 November 2017 |archive-url=https://web.archive.org/web/20191201142309/https://books.google.com/books?id=nneBa6-mWfgC&pg=PA475 |archive-date=1 December 2019 |url-status=live}}{{Cite web |date=16 October 2009 |title=C++ in Coders at Work |url=https://gigamonkeys.wordpress.com/2009/10/16/coders-c-plus-plus/ |url-status=live |archive-url=https://web.archive.org/web/20171110061643/https://gigamonkeys.wordpress.com/2009/10/16/coders-c-plus-plus/ |archive-date=10 November 2017 |access-date=9 November 2017}} and Donald Knuth.{{Cite web |title=An Interview with Donald Knuth |url=https://drdobbs.com/an-interview-with-donald-knuth/184409858 |url-status=live |archive-url=https://web.archive.org/web/20210308031011/https://www.drdobbs.com/architecture-and-design/an-interview-with-donald-knuth/228700500 |archive-date=8 March 2021 |access-date=18 July 2021 |website=Dr. Dobb's}}{{Cite web |title=(La)TeX Navigator |url=http://tex.loria.fr/litte/knuth-interview |url-status=live |archive-url=https://web.archive.org/web/20171120163257/http://tex.loria.fr/litte/knuth-interview |archive-date=20 November 2017 |access-date=10 November 2017}} This is largely due to its manual memory management, which makes it vulnerable to buffer overflow bugs, which represent a security risk.{{Cite news |last=Claburn |first=Thomas |date=2 March 2025 |title=C++ creator calls for help to defend programming language from 'serious attacks' |url=https://www.theregister.com/2025/03/02/c_creator_calls_for_action/ |access-date=5 March 2025 |work=The Register}}

History

File:BjarneStroustrup.jpg

In 1979, Bjarne Stroustrup, a Danish computer scientist, began work on "{{visible anchor|C with Classes}}", the predecessor to C++.{{cite web|url = http://www.stroustrup.com/bs_faq.html#invention|title = Bjarne Stroustrup's FAQ: When was C++ invented?|first = Bjarne|last = Stroustrup|website = stroustrup.com|date = 7 March 2010|access-date = 16 September 2010|archive-date = 6 February 2016|archive-url = https://web.archive.org/web/20160206214150/http://www.stroustrup.com/bs_faq.html#invention|url-status = live}} 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 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.{{cite web|url = http://stroustrup.com/hopl-almost-final.pdf|title = Evolving a language in and for the real world: C++ 1991-2006|first = Bjarne|last = Stroustrup|access-date = 14 August 2013|archive-date = 20 November 2007|archive-url = https://web.archive.org/web/20071120015600/http://www.research.att.com/~bs/hopl-almost-final.pdf|url-status = live}} C was chosen because it 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.{{Citation needed|date=February 2024}}

Initially, Stroustrup's "C with Classes" added features to the C compiler, Cpre, including classes, derived classes, strong typing, inlining, and default arguments.{{cite web|last1=Stroustrup|first1=Bjarne|title=A History of C ++ : 1979− 1991|url=http://www.stroustrup.com/hopl2.pdf|access-date=18 July 2013|archive-date=2 February 2019|archive-url=https://web.archive.org/web/20190202050609/http://www.stroustrup.com/hopl2.pdf|url-status=live}}

File:20160121 CppFRUG Joel Falcou CppQuiz 3.jpg

In 1982, Stroustrup started to develop 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 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 was not yet an official standard.{{cite web|url = http://www.stroustrup.com/1st.html|title = The C++ Programming Language|edition = First|first = Bjarne|last = Stroustrup|access-date = 16 September 2010|archive-date = 9 August 2012|archive-url = https://web.archive.org/web/20120809032136/http://www.stroustrup.com/1st.html|url-status = live}} 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.{{cite web|url = http://www.stroustrup.com/2nd.html|title = The C++ Programming Language|edition = Second|first = Bjarne|last = Stroustrup|access-date = 16 September 2010|archive-date = 9 August 2012|archive-url = https://web.archive.org/web/20120809032141/http://www.stroustrup.com/2nd.html|url-status = live}} 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 {{nowrap|C++14}} update released in December 2014, various new additions were introduced in C++17.{{cite web |url=https://herbsutter.com/2016/06/30/trip-report-summer-iso-c-standards-meeting-oulu/ |title=Trip report: Summer ISO C++ standards meeting (Oulu) |last=Sutter |first=Herb |website=herbsutter.com |date=30 June 2016 |archive-url=https://web.archive.org/web/20161008031743/https://herbsutter.com/2016/06/30/trip-report-summer-iso-c-standards-meeting-oulu/ |archive-date=8 October 2016 |quote=the next standard after C++17 will be C++20}} After becoming finalized in February 2020,{{cite web|url=http://open-std.org/JTC1/SC22/WG21/docs/papers/2019/n4817.pdf|title=N4817: 2020 Prague Meeting Invitation and Information|last=Dusíková|first=Hana|date=2019-11-06|access-date=2020-02-13|archive-date=29 December 2019|archive-url=https://web.archive.org/web/20191229102449/http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2019/n4817.pdf|url-status=live}} a draft of the C++20 standard was approved on 4 September 2020, and officially published on 15 December 2020.{{cite web |title=Current Status |url=https://isocpp.org/std/status |website=isocpp.org |access-date=7 September 2020 |archive-date=8 September 2020 |archive-url=https://web.archive.org/web/20200908083135/https://isocpp.org/std/status |url-status=live }}{{cite web |title=C++20 Approved -- Herb Sutter |url=https://isocpp.org/blog/2020/09/cpp20-approved-herb-sutter |website=isocpp.org |access-date=8 September 2020 |archive-date=11 September 2020 |archive-url=https://web.archive.org/web/20200911150359/https://isocpp.org/blog/2020/09/cpp20-approved-herb-sutter |url-status=live }}

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".{{cite press release |url=https://www.nae.edu/177355.aspx |title=Computer Science Pioneer Bjarne Stroustrup to Receive the 2018 Charles Stark Draper Prize for Engineering |publisher=National Academy of Engineering |date=3 January 2018 |access-date=14 December 2021 |archive-url=https://web.archive.org/web/20180103190112/https://www.nae.edu/177355.aspx |archive-date=3 January 2018}}

In December 2022, C++ ranked third on the TIOBE index, surpassing Java for the first time in the history of the index. {{As of|2024|November}}, the language ranks second after Python, with Java being in third.{{cite web|url=https://www.tiobe.com/tiobe-index/|title=TIOBE Index for November 2024|author=TIOBE|date=November 2024|website=TIOBE.com|publisher=TIOBE Company|access-date=18 November 2024|archive-date=18 November 2024|archive-url=https://web.archive.org/web/20241118090936/https://www.tiobe.com/tiobe-index/|url-status=live}}

=Etymology=

According to Stroustrup, "the name signifies the evolutionary nature of the changes from C."{{cite web |url=http://www.stroustrup.com/bs_faq.html#name |title=Bjarne Stroustrup's FAQ – Where did the name "C++" come from? |access-date=16 January 2008 |archive-date=6 February 2016 |archive-url=https://web.archive.org/web/20160206214150/http://www.stroustrup.com/bs_faq.html#name |url-status=live }} 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, the language had been referred to as "new C" and "C with Classes"{{cite web|title=C For C++ Programmers|url=https://www.ccs.neu.edu/course/com3620/parent/C-for-Java-C++/c-for-c++-alt.html|publisher=Northeastern University|access-date=7 September 2015|archive-url=https://web.archive.org/web/20101117003419/http://www.ccs.neu.edu/course/com3620/parent/C-for-Java-C++/c-for-c++-alt.html|archive-date=17 November 2010|url-status=dead|df=dmy-all}} before acquiring its final name.

=Philosophy=

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

  • 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=

{{main|C++03|C++11|C++14|C++17|C++20|C++23|C++26}}

class="wikitable floatright" style="margin-left: 1.5em;"

|+C++ standards

scope="col"| Year

!scope="col"| ISO/IEC Standard

!scope="col"| Informal name

scope="row"| 1998

| | 14882:1998{{cite web|title=ISO/IEC 14882:1998|publisher=International Organization for Standardization|url=https://www.iso.org/iso/iso_catalogue/catalogue_ics/catalogue_detail_ics.htm?ics1=35&ics2=60&ics3=&csnumber=25845|access-date=23 November 2018|archive-date=15 January 2017|archive-url=https://web.archive.org/web/20170115080045/http://www.iso.org/iso/iso_catalogue/catalogue_ics/catalogue_detail_ics.htm?ics1=35&ics2=60&ics3=&csnumber=25845|url-status=live}} || C++98

scope="row"| 2003

| | 14882:2003{{cite web|title=ISO/IEC 14882:2003|publisher=International Organization for Standardization|url=https://www.iso.org/iso/iso_catalogue/catalogue_ics/catalogue_detail_ics.htm?ics1=35&ics2=60&ics3=&csnumber=38110|access-date=23 November 2018|archive-date=13 August 2021|archive-url=https://web.archive.org/web/20210813193332/https://www.iso.org/standard/38110.html|url-status=live}} || C++03

scope="row"| 2011

| | 14882:2011{{cite web|title=ISO/IEC 14882:2011|publisher=International Organization for Standardization|url=https://www.iso.org/iso/iso_catalogue/catalogue_ics/catalogue_detail_ics.htm?ics1=35&ics2=60&ics3=&csnumber=50372|access-date=23 November 2018|archive-date=27 May 2016|archive-url=https://web.archive.org/web/20160527084921/http://www.iso.org/iso/iso_catalogue/catalogue_ics/catalogue_detail_ics.htm?ics1=35&ics2=60&ics3=&csnumber=50372|url-status=live}} || C++11, C++0x

scope="row"| 2014

| | 14882:2014{{cite web|title=ISO/IEC 14882:2014|publisher=International Organization for Standardization|url=https://www.iso.org/iso/home/store/catalogue_ics/catalogue_detail_ics.htm?csnumber=64029&ICS1=35&ICS2=60|access-date=23 November 2018|archive-date=29 April 2016|archive-url=https://web.archive.org/web/20160429201210/http://www.iso.org/iso/home/store/catalogue_ics/catalogue_detail_ics.htm?csnumber=64029&ICS1=35&ICS2=60|url-status=live}} || C++14, C++1y

scope="row"| 2017

| | 14882:2017{{cite web|title=ISO/IEC 14882:2017|publisher=International Organization for Standardization|url=https://www.iso.org/standard/68564.html|access-date=2 December 2017|archive-date=29 January 2013|archive-url=https://web.archive.org/web/20130129110331/http://www.iso.org/iso/iso_catalogue/catalogue_tc/catalogue_detail.htm?csnumber=50372|url-status=live}} || C++17, C++1z

scope="row"| 2020

| | 14882:2020{{cite web|title=ISO/IEC 14882:2020|publisher=International Organization for Standardization|url=https://www.iso.org/standard/79358.html|access-date=16 December 2020|archive-date=16 December 2020|archive-url=https://web.archive.org/web/20201216154357/https://www.iso.org/standard/79358.html|url-status=live}} || C++20, C++2a

scope="row"| 2024

| | 14882:2024{{cite web|title=ISO/IEC 14882:2024|publisher=International Organization for Standardization|url=https://www.iso.org/standard/83626.html|access-date=21 October 2020}} || C++23, C++2b

scope="row" {{TBA}}

| | || C++26, C++2c

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.

File:C++ Standards Committee meeting - July 1996 Stockholm - Wednesday general session.jpg

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.{{cite web|url=https://herbsutter.com/2011/08/12/we-have-an-international-standard-c0x-is-unanimously-approved/|title=We have an international standard: C++0x is unanimously approved|website=Sutter's Mill|date=12 August 2011|access-date=23 November 2018|archive-date=28 June 2018|archive-url=https://web.archive.org/web/20180628182816/https://herbsutter.com/2011/08/12/we-have-an-international-standard-c0x-is-unanimously-approved/|url-status=live}} C++11 (14882:2011) included many additions to both 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.{{Cite web|url=https://channel9.msdn.com/Events/Build/2012/2-005|title=The Future of C++|via=channel9.msdn.com|access-date=23 November 2018|archive-date=23 October 2018|archive-url=https://web.archive.org/web/20181023213741/https://channel9.msdn.com/Events/Build/2012/2-005|url-status=live}} The Draft International Standard ballot procedures completed in mid-August 2014.{{Cite web|url=https://isocpp.org/blog/2014/08/we-have-cpp14|title=We have C++14! : Standard C++|website=isocpp.org|access-date=19 August 2014|archive-date=19 August 2014|archive-url=https://web.archive.org/web/20140819083101/https://isocpp.org/blog/2014/08/we-have-cpp14|url-status=live}}

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.{{Cite web|url=https://herbsutter.com/2017/07/15/trip-report-summer-iso-c-standards-meeting-toronto/|title=Trip report: Summer ISO C++ standards meeting (Toronto)|first=Herb|last=Sutter|date=15 July 2017|access-date=4 August 2017|archive-date=6 August 2017|archive-url=https://web.archive.org/web/20170806182136/https://herbsutter.com/2017/07/15/trip-report-summer-iso-c-standards-meeting-toronto/|url-status=live}}

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

  • ISO/IEC TR 18015:2006{{cite web|publisher=International Organization for Standardization|title=ISO/IEC TR 18015:2006|url=https://www.iso.org/standard/43351.html|access-date=15 February 2019|archive-date=15 January 2019|archive-url=https://web.archive.org/web/20190115203236/https://www.iso.org/standard/43351.html|url-status=live}} on the use of C++ in embedded systems and on performance implications of C++ language and library features,
  • ISO/IEC TR 19768:2007{{cite web|url=https://www.iso.org/standard/43289.html|title=ISO/IEC TR 19768:2007|publisher=International Organization for Standardization|access-date=15 February 2019|archive-date=4 March 2016|archive-url=https://web.archive.org/web/20160304045148/http://www.iso.org/iso/iso_catalogue/catalogue_ics/catalogue_detail_ics.htm?ics1=35&ics2=60&ics3=&csnumber=43289|url-status=live}} (also known as the C++ Technical Report 1) on library extensions mostly integrated into C++11,
  • ISO/IEC TR 29124:2010{{cite web|url=https://www.iso.org/standard/50511.html|title=ISO/IEC TR 29124:2010|publisher=International Organization for Standardization|access-date=15 February 2019|archive-date=12 January 2019|archive-url=https://web.archive.org/web/20190112054620/https://www.iso.org/standard/50511.html|url-status=live}} on special mathematical functions, integrated into {{nowrap|C++17}},
  • ISO/IEC TR 24733:2011{{cite web|url=https://www.iso.org/standard/38843.html|title=ISO/IEC TR 24733:2011|publisher=International Organization for Standardization|access-date=15 February 2019|archive-date=15 January 2019|archive-url=https://web.archive.org/web/20190115203556/https://www.iso.org/standard/38843.html|url-status=live}} on decimal floating-point arithmetic,
  • ISO/IEC TS 18822:2015{{cite web|url=https://www.iso.org/standard/63483.html|title=ISO/IEC TS 18822:2015|publisher=International Organization for Standardization|access-date=15 February 2019|archive-date=15 January 2019|archive-url=https://web.archive.org/web/20190115201441/https://www.iso.org/standard/63483.html|url-status=live}} on the standard filesystem library, integrated into C++17,
  • ISO/IEC TS 19570:2015{{cite web|url=https://www.iso.org/standard/65241.html|title=ISO/IEC TS 19570:2015|publisher=International Organization for Standardization|access-date=15 February 2019|archive-date=15 January 2019|archive-url=https://web.archive.org/web/20190115201509/https://www.iso.org/standard/65241.html|url-status=live}} on parallel versions of the standard library algorithms, integrated into C++17,
  • ISO/IEC TS 19841:2015{{cite web|url=https://www.iso.org/standard/66343.html|title=ISO/IEC TS 19841:2015|publisher=International Organization for Standardization|access-date=15 February 2019|archive-date=15 January 2019|archive-url=https://web.archive.org/web/20190115201300/https://www.iso.org/standard/66343.html|url-status=live}} on software transactional memory,
  • ISO/IEC TS 19568:2015{{cite web|url=https://www.iso.org/standard/65238.html|title=ISO/IEC TS 19568:2015|publisher=International Organization for Standardization|access-date=15 February 2019|archive-date=15 January 2019|archive-url=https://web.archive.org/web/20190115202436/https://www.iso.org/standard/65238.html|url-status=live}} on a new set of library extensions, some of which are already integrated into C++17,
  • ISO/IEC TS 19217:2015{{cite web|url=https://www.iso.org/standard/64031.html|title=ISO/IEC TS 19217:2015|publisher=International Organization for Standardization|access-date=15 February 2019|archive-date=15 January 2019|archive-url=https://web.archive.org/web/20190115201732/https://www.iso.org/standard/64031.html|url-status=live}} on the C++ concepts, integrated into C++20,
  • ISO/IEC TS 19571:2016{{cite web|url=https://www.iso.org/standard/65242.html|title=ISO/IEC TS 19571:2016|publisher=International Organization for Standardization|access-date=15 February 2019|archive-date=15 January 2019|archive-url=https://web.archive.org/web/20190115201226/https://www.iso.org/standard/65242.html|url-status=live}} on the library extensions for concurrency, some of which are already integrated into C++20,
  • ISO/IEC TS 19568:2017{{cite web|url=https://www.iso.org/standard/70587.html|title=ISO/IEC TS 19568:2017|publisher=International Organization for Standardization|access-date=15 February 2019|archive-date=15 January 2019|archive-url=https://web.archive.org/web/20190115202428/https://www.iso.org/standard/70587.html|url-status=live}} on a new set of general-purpose library extensions,
  • ISO/IEC TS 21425:2017{{cite web|url=https://www.iso.org/standard/70910.html|title=ISO/IEC TS 21425:2017|publisher=International Organization for Standardization|access-date=15 February 2019|archive-date=15 January 2019|archive-url=https://web.archive.org/web/20190115201957/https://www.iso.org/standard/70910.html|url-status=live}} on the library extensions for ranges, integrated into C++20,
  • ISO/IEC TS 22277:2017{{cite web|url=https://www.iso.org/standard/73008.html|title=ISO/IEC TS 22277:2017|publisher=International Organization for Standardization|access-date=15 February 2019|archive-date=15 January 2019|archive-url=https://web.archive.org/web/20190115202004/https://www.iso.org/standard/73008.html|url-status=live}} on coroutines, integrated into C++20,
  • ISO/IEC TS 19216:2018{{cite web|url=https://www.iso.org/standard/64030.html|title=ISO/IEC TS 19216:2018|publisher=International Organization for Standardization|access-date=15 February 2019|archive-date=15 January 2019|archive-url=https://web.archive.org/web/20190115201923/https://www.iso.org/standard/64030.html|url-status=live}} on the networking library,
  • ISO/IEC TS 21544:2018{{cite web|url=https://www.iso.org/standard/71051.html|title=ISO/IEC TS 21544:2018|publisher=International Organization for Standardization|access-date=15 February 2019|archive-date=15 January 2019|archive-url=https://web.archive.org/web/20190115201858/https://www.iso.org/standard/71051.html|url-status=live}} on modules, integrated into C++20,
  • ISO/IEC TS 19570:2018{{cite web|url=https://www.iso.org/standard/70588.html|title=ISO/IEC TS 19570:2018|publisher=International Organization for Standardization|access-date=15 February 2019|archive-date=15 January 2019|archive-url=https://web.archive.org/web/20190115201519/https://www.iso.org/standard/70588.html|url-status=live}} on a new set of library extensions for parallelism
  • ISO/IEC TS 23619:2021{{cite web|url=https://www.iso.org/standard/76425.html|title=ISO/IEC TS 23619:2021|publisher=International Organization for Standardization|access-date=11 October 2021|url-status=live|archive-url=https://web.archive.org/web/20181215153205/https://www.iso.org/standard/76425.html |archive-date=15 December 2018}} on new extensions for reflective programming (reflection),
  • ISO/IEC TS 9922:2024{{cite web |title=ISO/IEC TS 9922:2024 |url=https://www.iso.org/standard/83630.html |publisher=International Organization for Standardization |access-date=1 April 2025 |archive-url=https://web.archive.org/web/20250401173108/https://www.iso.org/standard/83630.html |archive-date=1 April 2025 |url-status=live}} on new set of concurrency extensions, and
  • ISO/IEC TS 19568:2024{{cite web |title=ISO/IEC TS 19568:2024 |url=https://www.iso.org/standard/86293.html |publisher=International Organization for Standardization |access-date=1 April 2025 |archive-url=https://web.archive.org/web/20250225094830/https://www.iso.org/standard/86293.html |archive-date=25 February 2025 |url-status=live}} on another new set of library extensions.

More technical specifications are in development and pending approval.

Language

{{Main|C++ syntax}}

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 is the basis of C++. Doing it efficiently is what distinguishes it from other languages."{{cite web|url=https://www.infoq.com/news/2015/04/stroustrup-cpp17-interview|author=B. Stroustrup (interviewed by Sergio De Simone)|date=30 April 2015|access-date=8 July 2015|title=Stroustrup: Thoughts on C++17 - An Interview|archive-date=8 July 2015|archive-url=https://web.archive.org/web/20150708132155/http://www.infoq.com/news/2015/04/stroustrup-cpp17-interview|url-status=live}}

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:{{Cite book |first=Bjarne |last=Stroustrup |year=2000 |page=46 |title=The C++ Programming Language |edition=Special |publisher=Addison-Wesley |isbn=0-201-70073-5 }}{{cite web |url=http://www.stroustrup.com/3rd_issues.html |title=Open issues for The C++ Programming Language (3rd Edition) |first=Bjarne |last=Stroustrup |access-date=5 May 2014 |archive-date=5 May 2014 |archive-url=https://web.archive.org/web/20140505221112/http://www.stroustrup.com/3rd_issues.html |url-status=live }}This code is copied directly from Bjarne Stroustrup's errata page (p. 633). He addresses the use of '\n' rather than std::endl. Also see [http://www.stroustrup.com/bs_faq2.html#void-main Can I write "void main()"?] {{Webarchive|url=https://web.archive.org/web/20200702224848/http://www.stroustrup.com/bs_faq2.html#void-main |date=2 July 2020 }} for an explanation of the implicit return 0; in the main function. This implicit return is not available in other functions.

  1. include

int main()

{

std::cout << "Hello, world!\n";

}

Standard library

File:ANSI ISO C++ WP.jpg

{{Main|C++ Standard Library}}

The C++ standard consists of two parts: the core language and the standard library. C++ programmers expect the latter on every major implementation of C++; it includes aggregate types (vectors, lists, maps, sets, queues, stacks, arrays, tuples), algorithms (find, for_each, binary_search, random_shuffle, etc.), input/output facilities (iostream, for reading from and writing to the console and files), filesystem library, localisation support, smart pointers for automatic memory management, regular expression support, multi-threading library, atomics support (allowing a variable to be read or written to by at most one thread at a time without any external synchronisation), time utilities (measurement, getting current time, etc.), a system for converting error reporting that does not use C++ exceptions into C++ exceptions, a random number generator, and a slightly modified version of the C standard library (to make it comply with the C++ type system).

A large part of the C++ library is based on the Standard Template Library (STL). Useful tools provided by the STL include containers as the collections of objects (such as vectors and lists), iterators that provide array-like access to containers, and algorithms that perform operations such as searching and sorting.

Furthermore, (multi)maps (associative arrays) and (multi)sets are provided, all of which export compatible interfaces. Therefore, using templates it is possible to write generic algorithms that work with any container or on any sequence defined by iterators.

As in C, the features of the library may be accessed by using the #include directive to include a standard header. The C++ Standard Library provides 105 standard headers, of which 27 are deprecated. With the introduction of modules in C++20, these headers may be accessed with import, and in C++23, the entire standard library can now be directly imported as module itself, with import std;. Currently, the C++ standard library provides two modules, std and std.compat (a compatibility module for std which exports C standard library facilities into the global namespace).

The standard incorporates the STL that was originally designed by Alexander Stepanov, who experimented with generic algorithms and containers for many years. When he started with C++, he finally found a language where it was possible to create generic algorithms (e.g., STL sort) that perform even better than, for example, the C standard library qsort, thanks to C++ features like using inlining and compile-time binding instead of function pointers. The standard does not refer to it as "STL", as it is merely a part of the standard library, but the term is still widely used to distinguish it from the rest of the standard library (input/output streams, internationalization, diagnostics, the C library subset, etc.).{{cite web |url=http://www.stlport.org/resources/StepanovUSA.html |author=Graziano Lo Russo |title=An Interview with A. Stepanov |year=2008 |access-date=8 October 2015 |website=stlport.org |archive-date=4 March 2009 |archive-url=https://web.archive.org/web/20090304120628/http://www.stlport.org/resources/StepanovUSA.html |url-status=live }}

Most C++ compilers, and all major ones, provide a standards-conforming implementation of the C++ standard library.

C++ Core Guidelines

The C++ Core Guidelines{{Cite web|url=https://isocpp.github.io/CppCoreGuidelines/|title=C++ Core Guidelines|website=isocpp.github.io|access-date=9 February 2020|archive-date=16 February 2020|archive-url=https://web.archive.org/web/20200216200622/http://isocpp.github.io/CppCoreGuidelines/|url-status=live}} are an initiative led by Bjarne Stroustrup, the inventor of C++, 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++.

The Core Guidelines were announced{{Cite web|url=https://isocpp.org/blog/2015/09/bjarne-stroustrup-announces-cpp-core-guidelines|title=Bjarne Stroustrup announces C++ Core Guidelines : Standard C++|website=isocpp.org|access-date=31 March 2020|archive-date=11 May 2020|archive-url=https://web.archive.org/web/20200511035635/https://isocpp.org/blog/2015/09/bjarne-stroustrup-announces-cpp-core-guidelines|url-status=live}} in the opening keynote at CPPCon 2015.

The Guidelines are accompanied by the Guideline Support Library (GSL),{{Cite web|url=https://github.com/microsoft/GSL|title=microsoft/GSL|date=18 July 2021|via=GitHub|access-date=18 July 2021|archive-date=18 July 2021|archive-url=https://web.archive.org/web/20210718130829/https://github.com/microsoft/GSL|url-status=live}} a header only library of types and functions to implement the Core Guidelines and static checker tools for enforcing Guideline rules.{{Cite web|url=https://docs.microsoft.com/en-us/cpp/code-quality/using-the-cpp-core-guidelines-checkers|title=Using the C++ Core Guidelines checkers|website=Microsoft Learn |access-date=31 March 2020|archive-date=13 August 2021|archive-url=https://web.archive.org/web/20210813193329/https://docs.microsoft.com/en-us/cpp/code-quality/using-the-cpp-core-guidelines-checkers?view=msvc-160|url-status=live}}

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.{{cite web |url=https://mentorembedded.github.io/cxx-abi/ |title=C++ ABI Summary |date=20 March 2001 |access-date=30 May 2006 |archive-date=10 July 2018 |archive-url=https://web.archive.org/web/20180710195559/https://mentorembedded.github.io/cxx-abi/ |url-status=live }}

=With C=

{{Main|Compatibility of C and C++}}

C++ is often considered to be a superset of C but this is not strictly true.{{cite web |url=http://www.stroustrup.com/bs_faq.html#C-is-subset |title=Bjarne Stroustrup's FAQ – Is C a subset of C++? |access-date=5 May 2014 |archive-date=6 February 2016 |archive-url=https://web.archive.org/web/20160206214150/http://www.stroustrup.com/bs_faq.html#C-is-subset |url-status=live }} 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.{{cite web |url=http://home.datacomm.ch/t_wolf/tw/c/c9x_changes.html |title=C9X – The New C Standard |access-date=27 December 2008 |archive-date=21 June 2018 |archive-url=https://web.archive.org/web/20180621084656/http://home.datacomm.ch/t_wolf/tw/c/c9x_changes.html |url-status=live }} 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).{{cite web |title=C++0x Support in GCC |url=https://gcc.gnu.org/projects/cxx0x.html |access-date=12 October 2010 |archive-date=21 July 2010 |archive-url=https://web.archive.org/web/20100721215324/http://gcc.gnu.org/projects/cxx0x.html |url-status=live }}{{cite web |title=C++0x Core Language Features In VC10: The Table |url=https://blogs.msdn.com/b/vcblog/archive/2010/04/06/c-0x-core-language-features-in-vc10-the-table.aspx |access-date=12 October 2010 |archive-date=21 August 2010 |archive-url=https://web.archive.org/web/20100821114635/http://blogs.msdn.com/b/vcblog/archive/2010/04/06/c-0x-core-language-features-in-vc10-the-table.aspx |url-status=live }}{{cite web |url=https://clang.llvm.org/cxx_status.html |title=Clang - C++98, C++11, and C++14 Status |publisher=Clang.llvm.org |date=12 May 2013 |access-date=10 June 2013 |archive-date=4 July 2013 |archive-url=https://web.archive.org/web/20130704124639/http://clang.llvm.org/cxx_status.html |url-status=live }} 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 utilize inline assembly to take advantage of its low-level functionalities, greater speed, and enhanced control compared to high-level programming languagesBokil, Milind A. (2021). "[https://www.researchgate.net/publication/354744729_Writing_Assembly_Routines_within_CC_and_Java_Programs Writing Assembly Routines within C/C++ and Java Programs]". ResearchGate. Retrieved 1 April 2025.{{cite journal | url=https://doi.org/10.1145/3689749 | doi=10.1145/3689749 | title=Extending the C/C++ Memory Model with Inline Assembly | date=2024 | last1=De Vilhena | first1=Paulo Emílio | last2=Lahav | first2=Ori | last3=Vafeiadis | first3=Viktor | last4=Raad | first4=Azalea | journal=Proceedings of the ACM on Programming Languages | volume=8 | pages=1081–1107 }} when optimizing for performance is essential. C++ provides support for embedding assembly language using asm declarationscppreference.com contributors. "[https://en.cppreference.com/w/cpp/language/asm asm declaration]". cppreference.com. Retrieved 1 April 2025., 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:{{Cite web |title=Extended Asm (Using the GNU Compiler Collection) |url=https://gcc.gnu.org/onlinedocs/gcc/Extended-Asm.html |website=GCC Online Documentation |publisher=GNU Project |access-date=1 April 2025}} Use the GCC extended inline assembly syntax. Using __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 widely adopted, including by IntelIntel Corporation. "[https://www.intel.com/content/www/us/en/docs/cpp-compiler/developer-guide-reference/2021-9/inline-assembly.html Inline Assembly]". Intel® C++ Compiler Classic Developer Guide and Reference, Version 2021.9. Retrieved 1 April 2025. and IBMIBM. "[https://www.ibm.com/docs/en/xl-c-aix/13.1.3?topic=statements-inline-assembly-extension Inline assembly statements (IBM extension)]". IBM Documentation. Retrieved 1 April 2025. 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.{{Cite web |title=Inline Assembler Overview |url=https://learn.microsoft.com/en-us/cpp/assembler/inline/inline-assembler-overview?view=msvc-170 |website=Microsoft Learn |publisher=Microsoft |access-date=1 April 2025}}
  • TI ARM Clang and Embedded Compilers:{{Cite web |title=Interfacing C and C++ With Assembly Language |url=https://software-dl.ti.com/codegen/docs/tiarmclang/compiler_tools_user_guide/compiler_manual/runtime_environment/interfacing-c-and-c-with-assembly-language-stdz0544217.html#interfacing-c-and-c-with-assembly-language |website=Texas Instruments |publisher=Texas Instruments Incorporated |date=23 February 2025 |access-date=1 April 2025}} 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.

1. Standalone assembly files – Assembly code is written separately and linked with C++ code.{{cite web |url=https://wiki.osdev.org/C%2B%2B_to_ASM_linkage_in_GCC |title=C++ to ASM linkage in GCC |website=OSDev Wiki |access-date=1 April 2025}}

2. Inline assembly – Assembly code is embedded within C++ code using compiler-specific extensions.

;Example Code for ASM Compatibility

  • When calling an assembly function from C++, use extern "C" to prevent C++ name mangling.

//main.cpp

import std;

extern "C" int add_asm(int, int); // Declare the assembly function

int main() {

int result = add_asm(5, 7);

std::println("Result from ASM: {}", result);

return 0;

}

  1. asm code using RISC-V architecture

.section .text

.global add_asm

add_asm:

add a0, a0, a1 # Add first argument (a0) and second argument (a1), store in a0

ret # Return (a0 holds return value)

  • Global variables in assembly must be declared as extern in C++ and marked .global in assembly.

// main.cpp

import std;

extern "C" int global_var; // Declare global variable from assembly

int main() {

std::println("Global variable from ASM: {}", global_var);

return 0;

}

  1. asm using RISC-V architecture

.section .data

.global global_var

.align 4

global_var:

.word 42 # Define integer value

  • Inline assembly allows embedding ASM directly in C++ using the asm keyword.

//main.cpp (using GCC/CLANG compiler)

import std;

int main() {

int x = 10, y = 20, sum;

asm volatile (

"add %0, %1, %2"

: "=r" (sum) // Output operand (stored in a register)

: "r" (x), "r" (y) // Input operands (stored in registers)

);

std::println("Sum using inline ASM: {}", sum);

return 0;

}

See also

Footnotes

{{Reflist|group=note}}

{{Clear|right}}

References

{{Reflist}}

Further reading

{{Refbegin|30em}}

  • {{Cite book |first1=David |last1=Abrahams |author-link=David Abrahams (computer programmer) |first2=Aleksey |last2=Gurtovoy |title=C++ Template Metaprogramming: Concepts, Tools, and Techniques from Boost and Beyond |year=2005 |publisher=Addison-Wesley |isbn=0-321-22725-5 }}
  • {{Cite book |first=Andrei |last=Alexandrescu |author-link=Andrei Alexandrescu |year=2001 |title=Modern C++ Design: Generic Programming and Design Patterns Applied |publisher=Addison-Wesley |isbn=0-201-70431-5 }}
  • {{Cite book |first1=Andrei |last1=Alexandrescu |author-link=Andrei Alexandrescu |first2=Herb |last2=Sutter |author-link2=Herb Sutter|year=2004 |title=C++ Design and Coding Standards: Rules and Guidelines for Writing Programs |publisher=Addison-Wesley |isbn=0-321-11358-6 }}
  • {{Cite book |first=Pete |last=Becker |author-link=Pete Becker |year=2006 |title=The C++ Standard Library Extensions : A Tutorial and Reference |publisher=Addison-Wesley |isbn=0-321-41299-0 }}
  • {{Cite book |first=Frank |last=Brokken |year=2010 |title=C++ Annotations |publisher=University of Groningen |isbn=978-90-367-0470-0 |url=https://www.icce.rug.nl/documents/cplusplus/ |access-date=28 April 2010 |archive-date=28 April 2010 |archive-url=https://web.archive.org/web/20100428014354/http://www.icce.rug.nl/documents/cplusplus/ |url-status=live }}
  • {{Cite book |first=James O. |last=Coplien |author-link=James O. Coplien |orig-year=reprinted with corrections, original year of publication 1992 |date=1994 |title=Advanced C++: Programming Styles and Idioms |publisher=Addison-Wesley |isbn=0-201-54855-0 |url=https://archive.org/details/advancedcbsprogr00copl }}
  • {{Cite book |first=Stephen C. |last=Dewhurst |year=2005 |title=C++ Common Knowledge: Essential Intermediate Programming |publisher=Addison-Wesley |isbn=0-321-32192-8 }}
  • {{Cite book |author=Information Technology Industry Council |author-link=Information and Communications Technology Council |publisher=ISO/IEC |location=Geneva |title=Programming languages – C++ |id=14882:2003(E) |edition=Second |date=15 October 2003 }}
  • {{Cite book |first=Nicolai M. |last=Josuttis |title=The C++ Standard Library, A Tutorial and Reference |edition=Second|year=2012 |publisher=Addison-Wesley |isbn=978-0-321-62321-8}}
  • {{Cite book |first1=Andrew |last1=Koenig |author-link=Andrew Koenig (programmer) |first2=Barbara E. |last2=Moo |year=2000 |title=Accelerated C++ – Practical Programming by Example |publisher=Addison-Wesley |isbn=0-201-70353-X |url=https://archive.org/details/acceleratedcprac2000koen }}
  • {{Cite book |first1=Stanley B. |last1=Lippman |author-link=Stanley B. Lippman |first2=Josée |last2=Lajoie |first3=Barbara E. |last3=Moo |year=2011 |edition=Fifth |title=C++ Primer |url=https://archive.org/details/cprimer0000lipp_5thed |url-access=registration |publisher=Addison-Wesley |isbn=978-0-321-71411-4 }}
  • {{Cite book |first=Stanley B. |last=Lippman |year=1996 |title=Inside the C++ Object Model |publisher=Addison-Wesley |isbn=0-201-83454-5 }}
  • {{Cite book |first=Scott |last=Meyers |author-link=Scott Meyers |year=2005 |title=Effective C++ |edition=Third |publisher=Addison-Wesley |isbn=0-321-33487-6 |url=https://archive.org/details/effectivec55spec00meye }}
  • {{Cite book |first=Bjarne |last=Stroustrup|author-link=Bjarne Stroustrup |year=2013 |title=The C++ Programming Language |edition=Fourth |publisher=Addison-Wesley |isbn=978-0-321-56384-2 }}
  • {{Cite book |first=Bjarne |last=Stroustrup|author-link=Bjarne Stroustrup |year=1994 |title=The Design and Evolution of C++ |publisher=Addison-Wesley |isbn=0-201-54330-3 }}
  • {{Cite book |first=Bjarne |last=Stroustrup|author-link=Bjarne Stroustrup|year=2014 |title=Programming: Principles and Practice Using C++ |edition=Second |publisher=Addison-Wesley |isbn=978-0-321-99278-9 }}
  • {{Cite book |first=Herb |last=Sutter|author-link=Herb Sutter |year=2001 |title=More Exceptional C++: 40 New Engineering Puzzles, Programming Problems, and Solutions |publisher=Addison-Wesley |isbn=0-201-70434-X }}
  • {{Cite book |first=Herb |last=Sutter|author-link=Herb Sutter |year=2004 |title=Exceptional C++ Style |publisher=Addison-Wesley |isbn=0-201-76042-8 }}
  • {{Cite book |first1=David |last1=Vandevoorde |first2=Nicolai M. |last2=Josuttis |year=2003 |title=C++ Templates: The complete Guide |publisher=Addison-Wesley |isbn=0-201-73484-2 }}

{{refend}}