4 ms·
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.
by antirez 7y ago
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.
- electrograv 7y agoAs 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.
- jhallenworld 7y agoA main problem with C++ is that it uses .c_str() instead of something nice like .s for this.