Re:Simple Dynamic Strings library for C, compatible with nul...
by antirez on Tuesday December 03, 2019 @12:27PM
(#21694407)
One of the main points of the library is the ability to pass SDSs where char* is expected without doing anything. So this is a "feature" in the author's spirit.
[ Parent
| on HN ]
Re:Simple Dynamic Strings library for C, compatible with nul...
by electrograv on Tuesday December 03, 2019 @12:39PM
(#21694552)
As another reply has suggested, you can design the wrapper struct so you can simply write:
legacy_fn(my_text.s);
Versus the current: legacy_fn(my_text);
I don’t think saving two characters per legacy function call is even remotely worth the loss of static type safety (which risks serious memory corruption and/or security holes, which are entirely preventable at compile-time in this way).In fact, I even find the explicitness more pleasantly and clearly readable: I like being able to know at-a-glance when types are changing, especially in a language as unsafe as C.
Lastly, if you can tolerate just using some of C++‘s features, you can define a no-compromise solution: A type (still represented by a single pointer under-the-hood) that will implicitly convert (with zero runtime cost) into a C string but not vice versa.
Re:Simple Dynamic Strings library for C, compatible with nul...
by jhallenworld on Tuesday December 03, 2019 @06:29PM
(#21698044)
A main problem with C++ is that it uses .c_str() instead of something nice like .s for this.