Content deleted Content added
Undid revision 1291523541 by 2605:8D80:13E7:CA75:415B:B06A:A4F8:C2C3 (talk) |
m Removed erroneous space and general fixes (task 1) |
||
Line 3:
[[File:Orwell Dev-Cpp zh cn.jpg|thumb|300px|A snippet of C++ code]]
The '''syntax of C++''' is [[syntax|the set of rules]] defining how a [[C++]] program is written and compiled.
C++ syntax is largely inherited from the syntax of its ancestor language [[C (programming language)|C]], and has influenced the syntax of several later languages including but not limited to [[Java (programming language)|Java]], [[C Sharp (programming language)|C#]], and [[Rust (programming language)|Rust]].
Line 190:
C++ has two kinds of [[Comment (computer programming)|comments]]: ''traditional comments'' and ''end-of-line comments''.
Traditional comments, also known as block comments, start with <code>/*</code> and end with <code>*/</code>, they may span across multiple lines.
<syntaxhighlight lang="cpp">
Line 277:
=== 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 languages<ref name="Bokil2021">Bokil, 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 April 1, 2025.</ref><ref name="Vilhena2024">{{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 | arxiv=2408.17208 }}</ref> when optimizing for performance is essential. C++ provides support for embedding [[Assembly language|assembly]] language using asm declarations,<ref name="cppreferenceAsm">cppreference.com contributors. "[https://en.cppreference.com/w/cpp/language/asm asm declaration]". ''cppreference.com''. Retrieved April 1, 2025.</ref>
==== Variations across compilers ====
Different C++ compilers implement inline assembly in distinct ways.
*GCC ([[GNU Compiler Collection]]) and [[Clang]]
*MSVC ([[Microsoft Visual C++]]): The inline assembler is built into the compiler. Previously supported inline assembly via the <syntaxhighlight lang="C++" inline>__asm</syntaxhighlight> keyword, but this support has been removed in 64-bit mode, requiring separate .asm modules instead.<ref>{{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}}</ref>
*TI ARM Clang and Embedded Compilers
▲*MSVC ([[Microsoft Visual C++]]): The inline assembler is built into the compiler. Previously supported inline assembly via the <syntaxhighlight lang="C++" inline>__asm</syntaxhighlight> keyword, but this support has been removed in 64-bit mode, requiring separate .asm modules instead<ref>{{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}}</ref>.
▲*TI ARM Clang and Embedded Compilers <ref>{{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=February 23, 2025 |access-date=April 1, 2025}}</ref>: 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.
2. [[Inline assembly]] – Assembly code is embedded within C++ code using compiler-specific extensions.
Line 509 ⟶ 507:
== Concepts ==
{{main|Concepts (C++)}}
Concepts are an extension to the [[template (C++)|templates]] feature provided by the [[C++]] programming language. Concepts are named [[Boolean value|Boolean]] predicates on template parameters, evaluated at [[compile time]]. A concept may be associated with a template ([[class (C++)|class]] template, [[function (computer programming)|function]] template, [[member function]] of a class template, [[Template
The main uses of concepts are:
Line 528 ⟶ 526:
* <code>C1</code>: A type-constraint. This kind replaces <code>class</code> or [[Typename|<code>typename</code>]] for declaring a [[TypeParameter|type template parameter]]. When using a concept instead of the former two the type is constraint.
* <code>C2</code>: A requires-clause. Whenever a type-constraint does not work, for example, because the concept takes multiple parameters, a requires-clause can be used to apply more elaborated constraints.
* <code>C3 / C4</code>: A constrained placeholder type. The same syntax is available for [[
* <code>C5</code>: A trailing requires-clause. This form is similar to <code>C2</code> with one notable exception. A trailing requires-clause can be applied to a function in a class template. This allows the function to remain a regular, template-free function, which can be enabled or disabled depending on the functions trailing requires-clause.
Line 536 ⟶ 534:
=== Headers ===
{{see also|Include directive}}
Traditionally (prior to [[C++20]]), code inclusion in C++ followed the ways of C, in which code was imported into another file using the preprocessor directive <code>#include</code>, which would copy the contents of the file into the other file.
Traditionally, C++ code would be divided between a header file (typically with extension {{mono|.h}}, {{mono|.hpp}} or {{mono|.hh}}) and a source file (typically with extension {{mono|.cpp}} or {{mono|.cc}}). The header file usually contained declarations of symbols while the source file contained the actual implementation, such as function implementations. This separation was often enforced because <code>#include</code>ing code into another file would result in it being reprocessed for each file it was included by, resulting in increased compilation times if the compiler had to reprocess the same source repeatedly.
Headers often also forced the usage of [[include guard|{{mono|#include}} guards]] or [[pragma once|{{mono|#pragma once}}]] to prevent a header from potentially being included into a file multiple times.
|