Hacker News new | past | comments | ask | show | jobs | submit
Try it on Compiler Explorer: https://godbolt.org/z/91dj5jeGW

Check out the source code: https://github.com/RyanJK5/rjk-duck

An include with a HTTP URL is a scary abomination straight put of hell. Please tell me that this is a compiler explorer specialty (which would still be cursed, but in a cool way) and not a GCC feature (which would be an absolute nightmare).
Yes, it's a Compiler Explorer feature. URL includes get rewritten client-side to work as if they were additional files supplied by the user. PR adding the current behavior is at [0], while I think the downloading code itself is at [1].

[0]: https://github.com/compiler-explorer/compiler-explorer/pull/...

[1]: https://github.com/compiler-explorer/compiler-explorer/blob/...

in the first example:

```

10: rjk::duck<Container> c{std::vector<int>{1, 2, 3}};

11: c.size(); // 3

12:

13: c = std::string{"hello"};

```

Does the assignment on line 13 call the destrucor for the vector of ints created on line 10?

Yes. duck takes ownership of the vector by moving it into its internal storage.

As a bonus, if you tried passing in an lvalue, it will reject the input unless you add the "copyable" trait, so it ends up mitigating some hidden copies.

Well, you’ve certainly convinced me to read your library.

Thanks for the blog post.

No problem! Hope you enjoy :)
Is there another option? Or were you asking if it leaks memory?

Maybe you were asking if it implements custom destructors? GC?

> Or were you asking if it leaks memory?

yeah,

If variable c where of type void* instead of duck<Container>, the assignment on line 13 would leak the memory used by the vector<int>.