Skip to main content
15 events
when toggle format what by license comment
Jan 20, 2022 at 22:14 comment added T.E.D. @DonHatch - That seems to be a fundamental design problem with string_view. Its effectively a pointer to the string object its created from, created as a manual developer optimization. So the developer is forced to be cognizant of how its going to be used.
May 1, 2021 at 0:37 comment added Don Hatch @Deduplicator It mirrors a major design point in some cases. In many others, it does not; it varies from being a major design point, to a minor one, to an implementation detail (which may vary from implementation to implementation), in which case the question of how to make the interface decision remains unanswered.
Apr 11, 2021 at 13:14 comment added Deduplicator @DonHatch That "implementation-detail" mirrors a major design-point, and is thus quite stable and non-surprising.
Jan 18, 2021 at 23:40 comment added Don Hatch So your recommendation is to make this interface decision based on an implementation detail of the function (that is, whether the function happens to store a copy of the input string value)? Really?
Jul 7, 2020 at 12:08 comment added Adrian McCarthy This is a great answer, but I think it needs a third case to recommend what to do when the function doesn't require taking ownership or reading the string but simply passes it on to another function that may do 1 or 2.
Dec 17, 2019 at 13:50 comment added Mgetz @ceztko you're obviously free to do that, I would generally suggest against it because it obfuscates the lifetime of the string_view by creating a temporary. Compilers can easily optimize most of this away, but clear lifetime is at least for me a must since this is a non-owning structure. I want the API to be clear I'm giving this as a borrow only.
Dec 17, 2019 at 13:47 comment added ceztko Ok, I asked if it was dangerous in some cases: since it's not, I think I will prefer to keep passing by const reference in most cases for better api clarity unless there are performance critical hot paths.
Dec 17, 2019 at 13:32 comment added Mgetz @ceztko it's completely unnecessary and adds an extra indirection when accessing the data.
Dec 16, 2019 at 23:01 comment added ceztko What's the problem of passing const std::string_view & in place of const std::string &?
May 13, 2019 at 7:14 comment added v.oddou Why const, non-ref ? The parameter being const is up to the specific use, but in general is reasonable as non-const. And you missed 3. Can accept slices
Jan 18, 2018 at 15:07 vote accept T.E.D.
Jan 16, 2018 at 21:02 comment added Mgetz @T.E.D. if you're just reading the value then the value will outlast the call. If you're taking ownership then it needs to outlast the call. Hence why I addressed both cases. The conversion operator just deals with making std::string_view easier to use. If a developer mis-uses it in an owning situation that's a programming error. std::string_view is strictly non-owning.
Jan 16, 2018 at 21:00 comment added T.E.D. Just to clarify one point, I'm thinking such a conversion operator would also take care of the worst of the lifetime issues, by making sure your RHS string value stays around for the entire length of the call?
Jan 16, 2018 at 20:50 history edited Mgetz CC BY-SA 3.0
added 57 characters in body
Jan 16, 2018 at 20:45 history answered Mgetz CC BY-SA 3.0