> ## Documentation Index
> Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt
> Use this file to discover all available pages before exploring further.

# C++ Subscript

The subscript operator (`[]`) is a postfix operator used to access elements of an array, a pointer to contiguous memory, or a user-defined container. In its built-in form, it is syntactic sugar for pointer arithmetic and dereferencing.

## Built-in Mechanics

For built-in types, the expression `E1[E2]` is strictly evaluated as `*((E1) + (E2))`. According to the C++ standard, one of the expressions must be a pointer to a completely-defined object type (or an array that decays to a pointer), and the other must be of an unscoped enumeration or integral type.

Because addition is commutative, the subscript operator is also commutative for built-in types.

```cpp theme={"dark"}
int main() {
    int arr[5] = {10, 20, 30, 40, 50};
    int* ptr = arr;

    // The following expressions are strictly equivalent:
    arr[2];   // Evaluates to *(arr + 2)
    ptr[2];   // Evaluates to *(ptr + 2)
    2[arr];   // Evaluates to *(2 + arr)
    
    return 0;
}
```

The built-in operator does not perform bounds checking. Accessing an index outside the allocated memory range results in undefined behavior.

## Operator Overloading

For user-defined types, `operator[]` can be overloaded to provide custom indexing semantics.

**Overloading Rules:**

1. It must be implemented as a member function. It cannot be a free (non-member) function. As of C++23, it may be implemented as a `static` member function; prior to C++23, it was strictly required to be a non-static member function.
2. It typically returns an lvalue reference (`T&`) to allow the expression to be used on the left side of an assignment.
3. It is standard practice to provide both a `const` and a non-`const` overload to support mutable and read-only access depending on the constness of the object instance.
4. Prior to C++23, it must accept exactly one parameter. The parameter can be of any type (e.g., `std::string` for associative containers).

```cpp theme={"dark"}
#include <cstddef>

class VectorWrapper {
    int* buffer;
    std::size_t size;

public:
    // Non-const overload: returns an lvalue reference for modification
    int& operator[](std::size_t index) {
        return buffer[index]; 
    }

    // Const overload: returns a const lvalue reference for read-only access
    const int& operator[](std::size_t index) const {
        return buffer[index];
    }
};
```

## C++23 Multidimensional Subscript Operator

Starting with C++23, the subscript operator can accept zero or multiple arguments, enabling direct multidimensional indexing without relying on chained operators (e.g., `[][]`) or the function call operator (`()`).

```cpp theme={"dark"}
#include <cstddef>

class Matrix {
    double* data;
    std::size_t cols;

public:
    // C++23 multidimensional subscripting
    double& operator[](std::size_t row, std::size_t col) {
        return data[row * cols + col];
    }
};

// Syntax usage:
// matrix[1, 2] = 3.14;
```

<div
  style={{ 
display: "flex", 
justifyContent: "space-between", 
alignItems: "center", 
maxWidth: "754px", 
padding: "1rem 0",
marginBottom: "24px"
}}
>
  <span style={{ fontWeight: "bold", fontSize: "1.25rem", color: "var(--tw-prose-headings)", fontFamily: "Inter, ui-sans-serif, system-ui, sans-serif" }}>Tired of Poor C++ Skills? Fix That With Deep Grasping!</span>

  <a
    href="https://syntblaze.com"
    target="_blank"
    style={{ 
  marginLeft: "24px",
  textDecoration: "none", 
  backgroundColor: "#007AFF",
  color: "#ffffff", 
  padding: "6px 16px", 
  borderRadius: "16px",
  fontSize: "0.9rem",
  fontWeight: "600",
  textAlign: "center",
  transition: "background-color 0.2s ease"
}}
  >
    Learn More
  </a>
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/skill-tracking.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=b9b0305c93bb501c9e767b5c76c88835" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/skill-tracking.png" />

  <img src="https://mintcdn.com/syntblazellc/23tyuOzaWS88qFlc/images/nuggets.png?fit=max&auto=format&n=23tyuOzaWS88qFlc&q=85&s=c86c80197299762989e9b882419b2109" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/nuggets.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/bite-sized-exercises.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=a65f9a38c37ff28ab73ed783c53c60e3" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/bite-sized-exercises.png" />
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap", marginTop: "12px" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/mastery-chain.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=748a1763454713e679260fbb95f154a2" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/mastery-chain.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-previews.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=242f61448ff5dd6deaaab2dccc13b507" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-previews.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-explanations.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=cf0fc1c31f9cd0fc26716781be05fbc9" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-explanations.png" />
</div>
