- LTTNG_ASSERT(index < ContainerOperations::size(_container));
- return ContainerOperations::get(_container, index);
+ /*
+ * To share code between the const and mutable versions of this operator, 'this'
+ * is casted to a const reference. A const_cast then ensures that a mutable
+ * reference (or pointer) is returned.
+ *
+ * We typically avoid const_cast, but this is safe: if the user is calling the
+ * mutable version of this operator, it had a mutable object anyhow.
+ *
+ * For more information, see Item 3 of Effective C++.
+ */
+ const auto& const_this = static_cast<const random_access_container_wrapper&>(*this);
+
+ /* NOLINTNEXTLINE(cppcoreguidelines-pro-type-const-cast) */
+ return const_cast<typename std::conditional<std::is_pointer<ElementType>::value,
+ ElementType,
+ ElementType&>::type>(const_this[index]);