C++ std::string_view equivalent in D?

Jonathan M Davis newsgroup.d at jmdavisprog.com
Wed Feb 21 10:23:23 UTC 2018


On Wednesday, February 21, 2018 10:17:55 0xFFFFFFFF via Digitalmars-d-learn 
wrote:
> On Wednesday, 21 February 2018 at 09:21:58 UTC, 0xFFFFFFFF wrote:
> > What is the equivalent of C++17 std::string_view (an object
> > that can refer to a constant contiguous sequence of char-like
> > objects with the first element of the sequence at position
> > zero) in D?
> >
> > PS: I'm getting back to D after years (since DMD 1 days). A lot
> > changes since v1.0.
>
> Wow! Thanks guys.
> Those are handy, but I specifically want to do something like:
>
> ```
> string_view sv = "some string";
> ```
> So I can query it's size, make it point to sth else, use it in
> callbacks etc. Of course I'm aware of scoping etc...
>
> I'm NOT doing any dynamic allocations, I just want a situation
> where a string allocation string would be a little bit expensive.
>
>
> [I believe I still have to study D more in that case to come up
> with sth better, enjoying my ride so far]

As I said in my previous response, read this article:

https://dlang.org/articles/d-array-article.html

You clearly do not understand at all how strings work in D. Strings in D
basically _are_ string_views where most strings have the actual memory
buffer managed by the GC. If you do

string sv = "some string";

then _zero_ heap allocations take place.

- Jonathan M Davis



More information about the Digitalmars-d-learn mailing list