2

Are there any use cases for std::vector::reserve() on an r-value std::vector, or is reserve() not l-value ref-qualified only because of backward compatibility?

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
blonded04
  • 147
  • 9
  • is there any method of `std::vector` that is l/r ref qualified? – 463035818_is_not_an_ai Aug 30 '22 at 11:49
  • no there is not, but if i'm implementing `vector`-wrapper with some extra invariants needed for my project i want to know whether i should ref-qualify `reserve` or i don't – blonded04 Aug 30 '22 at 11:57
  • 2
    Is it worth someone time - writing the proposal; checking existing code bases (for breaking changes) and then steering it through the committee stages, revisions etc. `std::vector` pre-dates C++11 where reference qualifiers were added to the language. – Richard Critten Aug 30 '22 at 11:58
  • 3
    It's a valid point. The std::vector could have its member functions carefully reference-qualified. I presume that hasn't been done because there wasn't a pressing need to qualify them. Typically, `const` correctness is very important, but reference-qualified correctness is something that useful for when it's really needed rather than as a matter of course. – Eljay Aug 30 '22 at 11:58
  • 1
    I don't see a problem to have it ref-qualified in your wrapper. There is no issue to make it more restrictive than the original one (the opposite would have been a problem). The same way it's not an issue to treat a non-const as a const, but it would be an issue to do the opposite :) – Fareanor Aug 30 '22 at 12:08
  • imho `std::vector&&` shouldn't be really able to do `reserve` because it should be forbidden, why would programmer call it? i don't know whether someone can call reserve on r-value of my wrapper, because i don't know any usecases, that's why i'm asking D: – blonded04 Aug 30 '22 at 12:08
  • Actually, calling `reserve()` on an r-value is at most a waste of time (useless operation) but it won't compromise the code integrity so not sure if it should be forbidden. But yes, it's pointless – Fareanor Aug 30 '22 at 12:10
  • 1
    @blonded04 as I said any change to the spec needs to go through the process, needs a proposer and a written proposal and then to get time on the Standard's committee . Theses are all volunteers so if it's not been proposed no feels it's worth their time. – Richard Critten Aug 30 '22 at 12:11
  • @Fareanor i mean if you are sleepy you can accidentally call `vector(size, createFoo()).reserve(capacity)` and sitting absolutely clueless looking at extra allocations – blonded04 Aug 30 '22 at 12:13
  • @blonded04 if you feel strongly that the language should change then you should propose it. – Richard Critten Aug 30 '22 at 12:15
  • 1
    @blonded04 Is that really an issue? If you are going so far to write `vector(size, createFoo()).reserve(capacity)` then you are going to want to use that vector later and you'll get a nice compiler error saying you can't. – NathanOliver Aug 30 '22 at 12:25
  • This is basically the same question as https://stackoverflow.com/questions/61141894 (If you agree, I can close the question as a duplicate), with the difference being the specific member function that is not ref-qualified. There is a related [proposal](https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2009/n2819.html) to ref qualify assignment operators for all `std` types (where it makes sense). – cigien Aug 30 '22 at 15:41
  • @cigien yeah, feel like it's a bit duplicate, but thought maybe there is a weird way to use `reserve` on r-value – blonded04 Aug 31 '22 at 10:36

0 Answers0