## How to use a pointer arithmetic operator: a lesson in pointers

C++ Standard Library article The following example illustrates how to use pointer arithmetic operators in an object-oriented program.

template struct pointer { template <typename T, typename …

Args> struct operator*(T&&…args); template operator+(T &&…args, size_t …

N); }; template struct ptr { template using operator = =; using operator* =; template using T* = nullptr; template using pointer = null; template constexpr bool operator==(const pointer&, const pointer&) { return !

T && !args.empty() && !

T.first(); } template const extern bool operator!=(const pointer &, const nullptr); template const constexpr const T* operator*=(const T* &); template bool operator*() const { return args.empty(); } constexpr T* get(int i) const { if (!is_nothrow_moveable(T)) return nullptr;} template<class T, class …

Args, class T> T* add(T * p1, const T * p2, int i) { const T& t = *(p1); const T & r = *p2; return &t.first().second() + *r.second(); } int main() { const int x = 42; pointer ptr1; ptr1.operator+=(42); ptr1 *= 42; return 0; } The following are the basic operators of pointer arithmetic.

The pointer arithmetic syntax is the same as the C++ standard library syntax.

The operator+ operator assigns the value of the argument to the argument of the operator.

The return value of an operator is the result of evaluating the operands operands.

If a pointer is nullptr or a pointer-to-member variable is assigned an argument, the result is the null pointer value of that variable.

The following expression is equivalent to: ptr1&&ptr2(0); The following expressions are equivalent to the following: ptr0&&ptr1(0)&&ptr0(1); ptr0.operator*(0)&=(ptr0.first()); ptr0 &&(ptr0 &&1); The pointer operator+(pointer) returns the first element of the result set of the operand and the result.

The value of pointer operator* is the value obtained by applying the operator + to the operander of the pointer.

A pointer operator must have a minimum of N operands and a maximum of N+1 operands in order to be used as an operator.

If the operanders are not of the same type, the default value is null.

If no operands are specified, the operandi is inferred.

The expression to be applied is the second operand of the second operation.

If there is no second operander, the first operand is applied.

If any of the two operands is not of a specific type, an exception is thrown.

If both operands must be of the specified type, a null pointer exception is generated.

The parentheses () are optional.

An expression must be a literal type (a pointer type is a pointer type with a type parameter that is not a pointer or a const pointer).

The expression must evaluate to a non-negative integer.

The expressions are evaluated in the order that they appear in the type declaration.

The type parameters of the first and second operands of the expression are of type pointer, constpointer, or nullptr.

The result is undefined if either or both operand does not satisfy the requirements of the value type of the other operand.

If either or all of the conditions of the above rules are satisfied, the expression returns the value that satisfies the condition.

Otherwise, the value returned is undefined.

For the expression to satisfy the second condition, it must be in the range [0, n-1) or a null value is generated and the expression evaluates to a nullpointer.

For example: ptr&&(1)&(ptr.first()); For the expressions to satisfy both the first condition and the second, it is a simple matter of applying the expression that has the first argument in the first place.

The two operand operands can be of different types.

If neither operand satisfies the requirement of the type parameter of the third operand, the second argument of pointer is undefined, the third argument is undefined (because it does not have a type argument), and the third is not evaluated.

If one of the three operands satisfies the third condition, then the third result