[Development] QList for Qt 6

Matthew Woehlke mwoehlke.floss at gmail.com
Thu Jun 13 00:23:04 CEST 2019


On 12/06/2019 18.14, Matthew Woehlke wrote:
> On 22/05/2019 12.41, Konstantin Tokarev wrote:
>> In the latter QList can be replaced with 
>> std::vector<std::unique_ptr<T>> or QVector<std::unique_ptr<T>>
>
> No, it can't. `QList<T>` has value semantics w.r.t. `T`. Your
> "alternatives" have pointer semantics.

And, to be clear... while having to write `*` and `->` is annoying, it
gets worse:

  good_list.append(value);

  // Never mind that we can't 'append'...
  bad_list.emplace_back(new /* AIEE! */ value_t{value});
  bad_list.emplace_back(std::make_unique<value_t>(value));

...and worse:

  auto evil = std::move(bad_list[i]);
  bad_list[i]->frob();

A `QList<T>` *always* has a value at a valid index. Your proposed
"alternatives" don't have that guarantee.

A `vector<std::unique_ptr<T>>` is simply **NOT** a replacement for
`QArrayList<T>`. Period.

-- 
Matthew



More information about the Development mailing list