Skip to content

Commit 45a9e72

Browse files
authored
Support Numeric IntPtr
1 parent 2a48db6 commit 45a9e72

1 file changed

Lines changed: 7 additions & 1 deletion

File tree

standard/unsafe-code.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -787,22 +787,28 @@ In an unsafe context, the `+` operator ([§12.13.5](expressions.md#12135-additio
787787
```csharp
788788
T* operator +(T* x, int y);
789789
T* operator +(T* x, uint y);
790+
T* operator +(T* x, nint y);
791+
T* operator +(T* x, nuint y);
790792
T* operator +(T* x, long y);
791793
T* operator +(T* x, ulong y);
792794
T* operator +(int x, T* y);
793795
T* operator +(uint x, T* y);
796+
T* operator +(nint x, T* y);
797+
T* operator +(nuint x, T* y);
794798
T* operator +(long x, T* y);
795799
T* operator +(ulong x, T* y);
796800
T* operator –(T* x, int y);
797801
T* operator –(T* x, uint y);
802+
T* operator -(T* x, nint y);
803+
T* operator -(T* x, nuint y);
798804
T* operator –(T* x, long y);
799805
T* operator –(T* x, ulong y);
800806
long operator –(T* x, T* y);
801807
```
802808

803809
There are no predefined operators for pointer addition or subtraction with native integer ([§8.3.6](types.md#836-integral-types)) offsets. Instead, `nint` and `nuint` values shall be promoted to `long` and `ulong`, respectively, with pointer arithmetic using the predefined operators for those types.
804810

805-
Given an expression `P` of a data pointer type `T*` and an expression `N` of type `int`, `uint`, `long`, or `ulong`, the expressions `P + N` and `N + P` compute the pointer value of type `T*` that results from adding `N * sizeof(T)` to the address given by `P`. Likewise, the expression `P – N` computes the pointer value of type `T*` that results from subtracting `N * sizeof(T)` from the address given by `P`.
811+
Given an expression `P` of a data pointer type `T*` and an expression `N` of type `int`, `uint`, `nint`, `nuint`, `long`, or `ulong`, the expressions `P + N` and `N + P` compute the pointer value of type `T*` that results from adding `N * sizeof(T)` to the address given by `P`. Likewise, the expression `P – N` computes the pointer value of type `T*` that results from subtracting `N * sizeof(T)` from the address given by `P`.
806812

807813
Given two expressions, `P` and `Q`, of a data pointer type `T*`, the expression `P – Q` computes the difference between the addresses given by `P` and `Q` and then divides that difference by `sizeof(T)`. The type of the result is always `long`. In effect, `P - Q` is computed as `((long)(P) - (long)(Q)) / sizeof(T)`.
808814

0 commit comments

Comments
 (0)