Re:Why Not Just Do Simple C++ RAII in C?
by hgs3 on Wednesday May 22, 2024 @04:39PM
(#40446212)
Namespaces are nice, but to my knowledge require name mangling which isn't a thing in C. I'm curious what you mean by "clean up the code significantly" and "confusing code paradigms" because in C you typically prefix your functions to prevent name collisions which isn't confusing or too noisy in my subjective opinion.
[ Parent
| on HN ]
Re:Why Not Just Do Simple C++ RAII in C?
by theeandthy on Wednesday May 22, 2024 @06:20PM
(#40447407)
Yeah you’re right. I guess folks who want C++ stuff should just use C++…
I guess I should have reworded. I don’t expect that feature in C, but if I were to reinvent C today I would keep it the same but add namespace and mangling.
Adding an explicit prefix to every function call is a lot boilerplate when it’s all added up.
Name mangling is an implementation detail to fit into UNIX linker design space, not the same approach as other compiled languages with modules, with their own linker.
Also name mangling (which in this case would simply be appending the namespace name to the identifier) would be trivially implementable in C.
In fact on some targets the assembler name of identifiers doesn't always match the C name already.
Although as someone almost always explicitly qualifies names, typing foo_bar is not very different from foo::bar; the only minor advantages are that you do not have to use foo:: inside the implementation of foo itself and the ability to use aliases.
> which in this case would simply be appending the namespace name to the identifier
surely not. How do you differentiate these two functions?
void fooN(void);
namespace N { void foo(void); }