RFR: 8189088: Add intrusive doubly-linked list utility [v3]

Kim Barrett kbarrett at openjdk.org
Mon Oct 9 06:25:20 UTC 2023


On Sat, 7 Oct 2023 21:28:36 GMT, Kim Barrett <kbarrett at openjdk.org> wrote:

>> src/hotspot/share/utilities/intrusiveList.hpp line 87:
>> 
>>> 85:  * iterators and access to const-qualified elements.  A const object cannot be
>>> 86:  * added to a list whose value type is not const-qualified, as that would be
>>> 87:  * an implicit casting away of the const qualifier.
>> 
>> Okay, I feel like this can be shortened signifcantly:
>> 
>>> A const-qualified type can be part of an IntrusiveList, then you only get const iterators and access to const elements. If you use a non-const type, then you can get both const and non-const iterators and access to elements. You can't add const values to a non-const list, as that would be implicitly casting away the const qualifier.
>
> That rewrite isn't correct.
> 
> You _can_ add const values to a non-const list. In fact, you can only add
> values (const or not) to a non-const list. A const list has type `const
> IntrusiveList<T, accessor>`, while a non-const list is similar but without the
> `const` qualifier.
> 
> What you can't do is add a const value to a (necessarily non-const) list of
> non-const elements.  So if T is an unqualified class type, then you can add
> const values to an `IntrusiveList<const T, accessor>` but not to an
> `IntrusiveList<T, accessor>`.

Is something like this more clear? In my experience, non-intrusive collections
with const-qualified elements are uncommon, so I found the implications here
not immediately obvious.


 * A const iterator has a const-qualified element type, and provides const
 * access to the elements of the associated list.  A non-const iterator has an
 * unqualified element type, and provides mutable element access.  A non-const
 * iterator is implicitly convertible to a corresponding const iterator.
 *
 * A const list provides const iterators and access to const-qualified
 * elements, and cannot be used to modify the sequence of elements.  Only a
 * non-const list can be used to modify the sequence of elements.
 *
 * A list can have a const-qualified element type, providing const iterators
 * and access to const-qualified elements.  A const object cannot be added to
 * a list with an unqualified element type, as that wuold be an implicit
 * casting away of the const qualifier.

-------------

PR Review Comment: https://git.openjdk.org/jdk/pull/15896#discussion_r1349894749


More information about the hotspot-dev mailing list