12 ms·
> What if we want to be able to store more than integers in our dynamic array ? [...] The array_push_back function also needs to be refactored in order to accou
by tines 10y ago
> What if we want to be able to store more than integers in our dynamic array ? [...] The array_push_back function also needs to be refactored in order to account for the size and type of what we store in the data buffer. A possible approach is to use a macro, instead of the original function:
This is a situation where C++ really shines: you can use C++ with templates and not only have much cleaner code, but also avoid forcing the compiler to inline every single call to array_push_back. And you don't even need to use anything more than structs and functions, you don't need to go "full OO".
I wish C programmers would be more open to C++ but it seems like they're for the most part pretty closed. That's probably the C++ community's fault, but I'm not sure how to make amends.
- noselasd 10y agoIn that regards, I also think C++ programmers should be more open to C programmers that just needs a few new language constructs. It is not an all or nothing approach. It's fine to write code that looks and feels mostly like C, but still takes advantage of a few nice C++ features.
- tines 10y agoI agree, which is why I mentioned that you can do what this article suggests without using anything except functions and structs. No classes or class features required.
- blub 10y agoThere are perfectly legitimate use-cases for such a "C+" style. The disadvantage is that it won't be considered "good C++ style" and would limit the attractiveness of the project to C++ programmers that enjoy modern C++. If one would include vector, string, array, smart pointers and maybe simple template code in C+ it would already have a big safety advantage over plain C.
- semi-extrinsic 10y ago> would limit the attractiveness of the project to C++ programmers that enjoy modern C++ As Linus wrote when someone told him on the mailing lists that Git should've be done in C++ """ Quite frankly, even if the choice of C were to do nothing but keep the C++ programmers out, that in itself would be a huge reason to use C. """
- pjmlp 10y agoYet he had to move to Qt for Substrate due to issues with Gtk.
- inDigiNeous 10y agoWell, Linus is a great C -programmer, so that's something he knows well and likes to protect and embrace.
- JoeAltmaier 10y agoSounds like a bitter old fossil. My first look at Linux, there were 200 sound-card drivers in the source base. Mostly identical except for the layout of bits in the control register. Any change to the api or conventions, 200 files had to be edited. An ideal place to refactor as a 'sound card' base class and derive classes to handle the (miniscule) differences. "Keeping new ideas away" is a good working definition of an old coot.
- st3fan 10y agoYou don't necessarily need C++ to implement a good driver design with base classes and minimal code duplication. It is perfectly possible in C.
- deleted 10y ago[deleted]
- Vindicis 10y agoThat's pretty much how I use C++. Granted, my knowledge of it is very minimal. I was forced into C++ because of my Broker's api so... One of my largest gripes making the switch, is the OO paradigm. I get it, I understand it, and I really want to love it. But the theory of it vs. the implementations that I've seen, ugh. And the number of ways you can initialize variables etc... just makes no sense to me. It's like they just keep adding new ways to do things, for no other reason than because they can. What's wrong with only have one way to do simple things like that? I don't know, maybe it's just me but I strive to keep my code as simple, concise as I possibly can. I have enough complexity to deal with solving problems than having to wrestle with my language on top of it. Just to note, I'm strictly talking about having to use other people's code merged with my own. If it was solely me writing, I'd just use C++ for some of the nice things built in and toss classes and all that into the fire. YMMV.
- jjawssd 10y agoWe need a book with modern best practices like what happened in the Javascript world with "JavaScript: The Good Parts" from which we can glean a clean effective dialect, leading to a new C++ uptake and renaissance similar to what happened in the Javascript world in 2008
- swift 10y agoThey're not really equivalent, but Scott Meyers' series of books on C++ are some of my favorite technical writing ever, and his most recent book "Effective Modern C++" should be on the shelf of every working C++ programmer.
- 72deluxe 10y agoI agree with this - I finished reading this just before last year and I cannot recommend this book enough. It really is excellent (including topics like reference collapsing, type deduction in different scenarios, idiosyncrasies of async tasks). A great book and essential.
- kgabis 10y ago
- pjmlp 10y agoThe thing is, we get dispointed when the code just looks like "C compiled with C++ compiler", with all security exploits that entails. A good example is how Turbo Vision, Object Windows Library, Visual Components Library, Qt look like and the concessions Microsoft had to make to Afx so that it got rebranded into MFC and appealed to the Windows C developers.
- beojan 10y agoThe idiosyncrasies in Qt etc. are because they were first designed when C++ was very young, and many compilers had poor support for the STL.
- pjmlp 10y agoYou misunderstood me. I was first talking about the set of frameworks that dared to embrace the then C++ best practices. All of them are older than C++98, or even before STL was being considered. Then I mentioned the fact that when Microsoft presented their Afx framework to their beta testers, mostly Windows C developers, it was considered too high level and it was rebuilt as MFC.
- 1wd 10y agoThe term "C with classes" is almost used as an insult in some circles. I wonder if "C++ without classes" would be a better idea.
- CyberDildonics 10y agoC++ without polymorphism or inheritance is how the people that I think are worth listening to seem to do things.
- 72deluxe 10y agoHow would that be a good idea? I don't understand how I could write effective software without polymorphism or inheritance, other than in tiny tiny applications.
- psyc 10y agoInheritance is syntactic sugar for composition. But, I think of it as reverse-SS, because I find composition much more readable.
- sidlls 10y agoI've written entire components for networked and multiplayer modes for games, and an algorithmic trading system that uses ML in C++ using just structs, unions, templates, functions and composition (no polymorphism or inheritance).
- CyberDildonics 10y agoThink about this: what does it give you besides generic execution and data when the size of the data varies? Templates already let you build generic data structures and algorithms when the size of the data on doesn't vary.
- gpderetta 10y agowithout runtime polymorphism. /pedantic++
- CyberDildonics 10y agoIt's always just one or two more features and before you know it - C++
- WalterBright 10y agoThe thing is, everyone has a different idea of what those one or two features should be! Even me, https://digitalmars.com/articles/b44.html https://digitalmars.com/articles/b44.html
- cefstat 10y agoYes, despite all the problems in C++ it's just a much better language than C for defining new types that are on par with "native types". std::vector has so many benefits over any possible C implementation that there is no real comparison. Genericity is one, no need to remember to free after use is another. By the way, why is this C implementation something that requires C99 or C11 features? It looked just like standard old-fashioned ANSI C to me.
- pskocik 10y agoI like my C alternative better. I pass growth factors/increments as parameters to the vector macros so that I can affect how it grows on capacity exhaustion during each call and I have macros for creating and closing uninitialized gaps. C++ loses on many potential optimizations by insisting that its types always be in fully well-defined states except inside methods. Moreover the particular GNU implementation of the STL on Linux completely fails to turn certain vectors methods into memsets, memcpies and memmoves where it could, which pesimizes those particular ops by like two decimal orders of magnitude. The insistence on using new/delete based allocators instead of reallocs is a significant pessimization too. Reallocs perform, on average, several tens of percents better than new allocs followed by copies. An even more noticable pessimization is in the compilation times. Including `<vector>` adds good chunks of a second to the build time of an average-length translation unit. In comparison, working with C, even with all my generics included, on top of a good build system makes me feel as if I was working with a scripting language. No lags.
- tines 10y agoYour criticisms of vector are well put, but if anything they're all really problems with a particular implementation of the standard library, not the C++ language. If you make your own libraries you're free to do whatever you want, and template metaprogramming can help you gain even more performance by easily using optimized implementations for specific types, etc.
- swift 10y agoThese are fair points and issues like these are why many larger projects have their own custom vector-like classes. One thing I did want to call attention to is your point that C++ best practices require that objects be in well-defined states except inside methods. That's true, although it's not a language requirement, but in many cases you can get the best of both worlds through the use of lambdas. A simple example to give the flavor: template <typename Function> void my_vector::munge(Function initialize_gap) { create_uninitialized_gap(); // Some of our elements now contain uninitialized memory. // We don't want to expose this to arbitrary code, but // it's OK to expose this to the function the user // provided specifically to handle this. initialize_gap(gap_begin(), gap_end()); // The gap is now initialized, so when we return from // this method, we'll be in a well-defined state. } This is the standard pattern you use for such things in functional languages, and it works very well. The idea is to ensure that intermediate states are only seen by code that explicitly expects to handle them.
- necessity 10y agoMore like the clusterfuck that the language is than the community.
- pskocik 10y agoIn my codebase, I use expression macros (with a little bit of __typeof__-based type checks) to do it fully generically. Much of what you think you need C++ for can be done almost just as succinctly on top of plain C (I'm talking automated scope cleanups, semi-automated error handling, many generic things) and the compile times fly. I agree C++ has had some good ideas. In fact, I originally wanted to use it. But I came to the conclusion that it's too bloated, and more importantly, fundamentally broken in certain ways (RAII, exceptions, even templates and namespaces), and I ended up emulating what I think the good parts of C++ are on top of plain C. When C++ programmers think C, they usually think lack of generics and lots of explicit manual, micromanagement and pointer arithmetic, but it can be a lot more than that.
- tines 10y agoI'm not familiar with this approach, but I think it would entail code size increase for each use of the macros, and a runtime overhead for the typeof checks, correct? If so, C++ has a nice advantage in that all the type checks are at compile-time so you don't pay any more for those, and also you don't duplicate binary code with macros.
- pskocik 10y ago__typeof_ is a compile-time feature. It's like C++'s decltype. (You can make C++'s auto out of it too.) There's is a potential for code size increases with this approach, that's true. But the potential also exists with inlinable vector methods.
- tines 10y agoYes, but for the vector methods, you leave that decision up to the compiler which should be able to take the best route.
- pskocik 10y agoTrue. However, I think in the case of the dynamic array, this effect is minor. The macros are small, and most of the time, they end up wrapped in a function anyway. But, admittedly,it doesn't scale well to very big generics, which should be always wrapped in a function. (Eventually I plan to shove a simple transpiler in front of all my C code and do this, along with namespacing, how I think it should be done.)
- throwaway89121 10y agoI want something like std::vector, but I don't want exceptions. How does C++ help in this case?
- ensiferum 10y agostd::vector is a container in a c++ standard library. C++ is a language. You're mixing apples and oranges. What I'm saying is that you can use C++ without having to use STL and you can use C++ just fine to implement your own exceptionless container if you want (and many such implementations exist already). And you can also compile with exceptions disabled. Should your code throw it will terminate the process.
- gwmarks 10y agoSo, garbage like "And you can also compile with exceptions disabled. Should your code throw it will terminate the process." is in the plus, and the parent comment is downvoted. The C++ community at its finest.
- tines 10y agoIt will help you either use std::vector with a custom allocator that does not throw exceptions, or else write your own non-throwing vector class with templates, which would, I hope, be more maintainable and easier to use correctly than an equivalent written in C.
- hendzen 10y agoCompile with -fno-exceptions
- 72deluxe 10y agoYou can use any part of C++ without having to use the rest of it. For example, you don't have to write your own std::allocator to use std::vector, nor do you have to use exceptions. You can use which bits of C++ that you want - that's its great strength, permitting low-level behaviour whilst supporting high level abstractions (and as simple or as complex as you like).
- jokoon 10y agoA solution might be a minimal subset of C++, so with STL containers, some syntactic sugar, easier and faster to compile (modules), etc. Basically what go and rust have achieved, except you keep most of the C/C++ syntax "taste", and you remove too high level stuff like templates and inheritance. Honestly I don't think templates are so useful, since most programmers already re-use STL containers, which are templates, but nobody really write relevant template classes. > That's probably the C++ community's fault The problem with C++ is backward compatibility with C and big corporations trying to not break their codebase. It makes it very hard to make the language evolve. For example D is a very good language, but it can't gain momentum as long as it doesn't have a real way to gain presence. We just have to wait that corporation clean their codebase to make room for C++ compilers to adapt, and then things should improve.
- tines 10y agoYou can't have the STL containers without templates though. If you want to special-case them in the compiler, then what about the algorithms library? If you want to keep the algorithms library, then what about your own code that implements a higher-level algorithm using the lower-level ones in the algorithms library? Without templates you have to write your algorithm once for every type of container, or else use indirect function calls which would not be good. So this won't really work well I think. For me, the only selling point of C++ is templates, and I've never written a nontrivial program that didn't use them (and I like to think that I don't just use them frivolously).
- sgeisenh 10y agoParametric polymorphism is a powerful abstraction. Makes for much more readable programs than ad-hoc polymorphism a la vanilla C.
- deleted 10y ago[deleted]
- 72deluxe 10y agoYou do realise that STL containers fundamentally rely on templates, right? We use loads of template classes at where I work. Our code would not be maintainable without them.
- mtdewcmu 10y agoThe chief benefits to pure C IMO are that it's more portable, compiles faster, and it's a simple and stable language. Writing C takes more effort in some cases, so it's a trade-off.
- maxlybbert 10y agoIt's goofy, but one of the reasons I'll pick C for a quick program is that both Windows and Linux will give better error messages for standard functions that fail and set errno than for equivalent C++ code that throws an exception. This isn't a question about the language, it's definitely a quality-of-implementation issue for standard exceptions to have what() give a useful message. Just to clarify perror will generally give a better error message in: FILE* fh = fopen("foo.txt", "w+"); if (!fh) { perror("unable to open foo.txt"); return 1; } Than what() will in: std::fstream fs; fs.exceptions(std::ifstream::failbit | std::ifstream::badbit); try { fs.open("too.txt"); } catch (const std::exception& ex) { std::cerr << "unable to open foo.txt: " << ex.what() << '\n'; } (Ignoring other issues such as whether exceptions are better than return codes, etc.)
- mtdewcmu 10y agoA lot of people seem to use stdio even in C++. Having comprehensible error messages is related to being a simple language, IMO. C++ compile-time errors are notorious for being unreadable. I'm not familiar with the output of what(), but I'd wager that the problem is a consequence of all the abstraction.
- rkv 10y ago>This is a situation where C++ really shines I agree but passing in the size is really not much of a deterrent.
- AlexeyBrin 10y ago> you can use C++ with templates and not only have much cleaner code, but also avoid forcing the compiler to inline every single call to array_push_back Actually, you really, really, really want to inline every call to push_back for std::vector. Here is a relevant presentation by Chandler Carruth https://www.youtube.com/watch?v=s4wnuiCwTGU https://www.youtube.com/watch?v=s4wnuiCwTGU . But, hey, you don't want to make the compiler work for you :).
- aceperry 10y agoI gave up on C++ right after I learned it. At the time, this was before the STL was part of the standard, it looked really cool but I could tell that it was easy to get into trouble and it would take years and years to be really good at it. When Java became popular, it looked like it solved a lot of the problems that I was afraid of with C++. But it was slow and really verbose compared to C++ and C. Recently, I heard someone say that today's C++ is not your grandfather's C++ and the speaker made some interesting comments about how much better it was. So I'm intrigued by it and definitely open to looking into the new C++.
- Joky 10y agoYep, worth giving another look: I couldn't stand C++ before C++11, and now I can't stand C anymore :)