4 ms·
Was your `std::string` SBO comment meant to relate to the `std::vector` quote? `std::vector` can't use SBO bc. `std::swap` on `std::vector`s can't invalidate po
by nikki93 6y ago
Was your `std::string` SBO comment meant to relate to the `std::vector` quote? `std::vector` can't use SBO bc. `std::swap` on `std::vector`s can't invalidate pointers to elements.
- saagarjha 6y agoI swapped std::string and std::vector when writing that comment :/ But still, there's no need for a vector to necessarily keep three pointers, it could use a pointer and two sizes for example.
- ReactiveJelly 6y agoIn Rust they call it "three pointers" because a size and a pointer are either both 32 bits or both 64 bits
- steveklabnik 6y agoI’ve never heard this term used, to be honest. The type name is “usize.”
- plorkyeran 6y agoWhether std::vector holds three pointers or a pointer and two sizes is entirely irrelevant. Either way sizeof(vector<T>) does not vary depending on T.
- saagarjha 6y agoIt is extremely relevant when responding to the claim "std::vector consists of three pointers".
- frutiger 6y agoOnly to pedants. Everyone knows that adding a pointer to a ptrdiff_t calculated from the same array gives you another pointer. If your real life benchmark heavily penalises this addition (and not the complementary subtraction when computing a size or a capacity) I ask you to volunteer it.
- nikki93 6y agoIt's not simply pedantic, you would be interested in knowing that if you are interested in the implementation of vector-like data structures or if you want to implement one yourself, and the only motivation for that isn't that you are penalized in some benchmark. In article about the details of C++, and especially in a footnote, it's relevant IMO. It does happen that the only thing the point in the article relies on is `sizeof`, for sure.
- saagarjha 6y agoIt is not pedantic to reply to "std::vector is three pointers" with "actually, it might use a size instead of a pointer". Replying to "std::vector is often three pointers, or something functionally equivalent" with that would just be wrong. I have not made any claims about performance, but if I was asked to I would say that the difference is likely so minor that it doesn't matter.