C++ std::string_view equivalent in D?

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


On Wednesday, February 21, 2018 09:21:58 0xFFFFFFFF via Digitalmars-d-learn 
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.

strings in D are that way by their very nature, because they're dynamic
arrays, and in D, dynamic arrays are just poiner and a length. Effectively,
a dynamic array is

struct DynamicArray(T)
{
    size_t length;
    T* ptr;
}

They're slices of some piece of memory - usually GC-allocated memory, though
you can slice any memory and get a dynamic array out of it. I'd suggest
reading this article:

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

It does not use the official terminology, because it refers to the
GC-managed buffer as the dynamic array rather than the T[] as being the
dynamic array (whereas the official terminology is that T[] is a dynamic
array, regardless of what memory it's a slice of), but otherwise, what it
says is quite good and should give you a solid idea of how arrays work in D.

- Jonathan M Davis



More information about the Digitalmars-d-learn mailing list