C++ std::string_view equivalent in D?

0xFFFFFFFF 0xFFFFFFFF at mail.com
Wed Feb 21 10:43:14 UTC 2018


On Wednesday, 21 February 2018 at 10:24:39 UTC, ketmar wrote:
> 0xFFFFFFFF wrote:
>
>> [...]
>
> and that is exactly what slices are for! ;-)
>
> you are probably better to use `const(char)[]`, tho. like this:
>
> 	// don't store `s`, as it's contents may change after exiting 
> of `myfunc`
> 	// (this is what `const` implies here)
> 	void myfunc (const(char)[] s) { ... }
>
> 	...
> 	string s = "test string";
> 	myfunc(s); // yep, this works
> 	s = s[2..4]; // this doesn't allocate
> 	myfunc(s);
> 	myfunc(s[3..6]); // or this, it doesn't allocate
> 	myfunc("abc"); // or this, doesn't allocate
>
> you got the idea.

Gotta save this too.

[BTW how do I post a thumbs up emoji]


More information about the Digitalmars-d-learn mailing list