Using "strcpy" to assign value to dynamic char array
pascal111
judas.the.messiah.111 at gmail.com
Tue Nov 2 01:03:51 UTC 2021
On Monday, 1 November 2021 at 21:32:21 UTC, Ali Çehreli wrote:
> On 11/1/21 2:01 PM, Ali Çehreli wrote:
>
> > The program is as incorrect as its C equivalent would be. ;)
>
> I wrote a cool function to make it easy to disregard memory
> safety:
>
> import std.stdio;
>
> auto assumedLength(S)(ref S slice) {
> struct LengthSetter {
> void opAssign(size_t length) {
> // Nooo! :)
> *cast(size_t*)(&slice) = length;
> }
> }
>
> return LengthSetter();
> }
>
> void main() {
> auto arr = [ 1, 2, 3 ];
> arr.assumedLength = 10;
> writeln(arr);
> }
>
> Joking aside, I liked the nested struct and its opAssign to
> mimic internal `arr.length = 42` syntax. (I know it involves a
> potentially expensive delegate but still...)
>
> Ali
This function seems smart and flexible and higher than my current
level, I'll study it.
More information about the Digitalmars-d-learn
mailing list