4 ms·
This is a neat C++ exercise, but when you're working daily with a language and intend to write maintainable code that's going to be around for a while, it's bes
by oppositelock 9y ago
This is a neat C++ exercise, but when you're working daily with a language and intend to write maintainable code that's going to be around for a while, it's best to stay within common, boring conventions of that language. In C++'s case, just stick to explicit RAII-based guard wrappers and your life will be a lot easier. Macro-based emulations of other language features can lead to lots of unexpected behavior.
Sorry for pooh-poohing someone sharing some ideas, but being someone who has to maintain long lived code in quite a few languages, code like this immediately sets off my alarm bells.
Don't get me started on the dude who use #define magic to make C look like Pascal.
- xyzzy_plugh 9y agoI've seen a lot of C++, and there's no consensus on what "boring conventions" are. Some places ban templates, some ban RAII. Some ban operator overloading. Some ban lambdas or C++11 features. Some ban macros. There's a lot of variety, and just because you use something like this in your library or project doesn't mean you can't come out and play. C++ is the most "be yourself" language I've encountered. There is no idiomatic code. Now, I get where you are coming from, and I'd love C++ a lot more if there was less feature abuse, but I've accepted that the ship has sailed.
- kentonv 9y agoI disagree. If you tell people that the only way they can do something "on scope exit" is to write a whole class with a destructor and manually capturing variables, the result is they won't bother. They'll just put the code at the end of the function, and say "I'm too lazy to make this exception-safe." This defer() macro is almost exactly what I've been using for the last 5 years or so: https://github.com/capnproto/capnproto/blob/master/c++/src/kj/common.h#L1451 https://github.com/capnproto/capnproto/blob/master/c++/src/k... And I can say pretty confidently that it's a really good idea to have. I'm sort of surprised, actually, that not everyone is already doing this...
- pjmlp 9y ago> Don't get me started on the dude who use #define magic to make C look like Pascal. I did it for memory allocation, because C designers weren't able to create something as simple as new, rather requiring us to keep using sizeof on malloc calls. That was only for an year, until I got hold of Turbo C++ and only dropped pure C when my teachers required me to deliver assignments in C. Thankfully most of them accepted C++ as well.