C++ syntax: Difference between revisions

Content deleted Content added
Tags: Mobile edit Mobile web edit
 
(7 intermediate revisions by 4 users not shown)
Line 38:
* <code>alignas</code>
* <code>alignof</code>
* <code>and</code>
* <code>and_eq</code>
* <code>asm</code>
* <code>auto</code>
* <code>bitand</code>
* <code>bitor</code>
* <code>bool</code>
* <code>break</code>
Line 53 ⟶ 49:
* <code>char32_t</code>
* <code>class</code>
* <code>compl</code>
* <code>concept</code>
* <code>const</code>
Line 90 ⟶ 85:
* <code>new</code>
* <code>noexcept</code>
* <code>not</code>
* <code>not_eq</code>
* <code>nullptr</code>
* <code>operator</code>
* <code>or</code>
* <code>or_eq</code>
* <code>private</code>
* <code>protected</code>
Line 128 ⟶ 119:
* <code>wchar_t</code>
* <code>while</code>
* <code>xor</code>
* <code>xor_eq</code>
{{div col end}}
 
The keyword [[restrict|<code>restrict</code>]], though present in C, is not standard in C++, though some compilers may support it. The keyword <code>fortran</code>, a conditionally supported keyword in C which denotes linkage for the [[Fortran]] programming language, is conditionally supported in C++.
 
=== Alternative operator keywords ===
The following words are reserved keywords, but are used as alternative spellings for operators and tokens that use non-ISO646 characters.
{{div col|colwidth=15em}}
* <code>and</code>
* <code>and_eq</code>
* <code>bitand</code>
* <code>bitor</code>
* <code>compl</code>
* <code>not</code>
* <code>not_eq</code>
* <code>or</code>
* <code>or_eq</code>
* <code>xor</code>
* <code>xor_eq</code>
{{div col end}}
 
=== Identifiers with special meaning ===
Line 570 ⟶ 575:
class MyObject {
private:
[[no_unique_address]]
int x;
public:
[[nodiscard]]
Line 644 ⟶ 650:
 
== Reflection ==
In addition to basic metaprogramming provided in header <code><type_traits></code>, [[C++26]] introduces compile-time reflection. Compile-time reflection capabilities can be accessed in header <code><meta></code> and declarations are stored in namespace <code>std::meta</code>.
[[C++26]] introduces compile-time reflection.
 
=== Annotations ===
Most declarations can nowhave beannotations attached with annotations, which are just values associated with that declaration. Like [[Java annotation]]s, annotations can be accessed using reflection. Annotations are different from attributes becauseas theyattributes alloware arbitraryprimarily constantsa means to becommunicate attachedinformation to the compiler, makingwhile themannotations customizableare toa programs,feature unlikeof attributes.reflection Furthermore,and attributesallow arearbitrary primarilyconstants aand meansmetadata to communicatebe attached, making them customisable to theprograms, compilerunlike attributes. This allows for bridging the communication between the library API and the user.
<syntaxhighlight lang="cpp">
enumusing class [[=custom::EnumFlag]] Toggle {Debug;
using custom::EnumFlag;
using custom::Rename;
 
enum class [[=EnumFlag]] Toggle: uint8_t {
Off,
On
};
 
struct [[=custom::Debug]] Person {
[[=custom::renameRename("full name")]]
std::string full_namefullName;
int age;
};
Line 662 ⟶ 672:
 
The annotations have no initial meaning unless some implementations use those annotations to identify some characteristics and features.
 
Creating an annotation to generate a specialisation for <code>std::formatter<T></code> is as follows:
<syntaxhighlight lang="cpp">
template <auto V>
struct Derived {};
 
template <auto V>
inline constexpr Derived<V> Derive;
 
inline constexpr struct {} Debug;
 
template <typename T>
requires (std::meta::has_annotation(^^T, Derive<Debug>))
struct std::formatter<T> {
// ...
};
 
struct [[=Derive<Debug>]] Point {
int x;
int y;
};
 
int main() {
Point p = Point { .x = 1, .y = 2 };
// prints p = Point { .x = 1, .y = 2 }
std::println("p = {}", p);
}
</syntaxhighlight>
 
== See also ==
* [[C++ standard library]]
* [[C syntax]]
* [[Java syntax]]
* [[C Sharp syntax|C# syntax]]
* [[Rust syntax]]