5 ms·
What I've learnt is that if you start writing C++ as a plan-old C, you quickly find yourself reaching for this and that feature from C++ that makes your job eas
by eots 11y ago
What I've learnt is that if you start writing C++ as a plan-old C, you quickly find yourself reaching for this and that feature from C++ that makes your job easier and your code more readable. Knowing where to stop is the tricky part.
- madmoose 11y agoI like to call that "C with std::vector"
- bluetomcat 11y agoCoding in plain C99 with some discipline can actually get you most of the benefits of C++, without any of its quirks. The "object-orientedness" of C++ really boils down to implicitly passing the this pointer to member functions, inheritance + virtual functions, and namespaces. All of these can be emulated in what I would call "C with discipline". Just be consistent in your naming conventions, always use a common prefix for globally visible functions and types, and use structs of function pointers whenever you really need virtual functions.
- checker659 11y agoWhat about templates? For some reason, C programmers only seem to comment about the OO parts of C++ and never talk about templates. When you bring variadic templates into the mix, it's just more awesomeness. And, before you mention it, use clang to avoid getting the page long templates related errors.
- bluetomcat 11y agoI haven't seen much template wizardry in the C++ codebases I've worked on. It can generally be seen in the standard headers. Type-generic data containers can still be implemented with void pointers and very little runtime overhead, for example: http://lxr.nginx.org/source/src/core/ngx_array.h http://lxr.nginx.org/source/src/core/ngx_array.h
- detrino 11y agoYou cannot implement cache efficient data structures using void pointers.
- f00fc0d3 11y agoC99 is nice, but C still lacks generic. I am not talking about C++ template nonsense, but not having to write max element search for every primitive type. I am working in DSP project which was started with typical C++ OO BS, finally with ended up with C compiled as C++ + some simple templates for cases as above.
- bluetomcat 11y agoUsing statement expressions and the typeof extension, you are able to write function-like, type-generic, scope-insensitive macros like this one: #define MAX(a,b) ({ \ typeof (a) _a = (a); \ typeof (b) _b = (b); \ _a > _b ? _a : _b; \ }) The inability to write template functions can be compensated by designing your function so that it takes a function pointer where the type-specific action occurs.
- nly 11y agoYour MAX has no respect for namespaces or scopes. A real world macro is probably going to be called MYLIBRARY_MAX to avoid this. Secondly the C++ lambda solution is just easier to read. auto max = [](auto a, auto b) { return (a > b) ? a : b; }; max (3.1415, 42); // returns double max (42ul, 78u); // returns unsigned long max (17, 16); // returns int
- f00fc0d3 11y agoTypeof is not standard and many DSP compilers do not implement that. Secondly I meant max search in array, not between to scalar values which is trivial. I really don't want to have for(...) inlined everywhere.
- stinos 11y agoI am not talking about C++ template nonsense, but not having to write max element search for every primitive type How is it template 'nonsense', when those templates do exactly that?
- f00fc0d3 11y ago