7 ms·
This is true. But C++'s destructor behaviour means you get this pattern without any thought, and consistently and reliably and deterministically, whereas other
by 72deluxe 6y ago
This is true. But C++'s destructor behaviour means you get this pattern without any thought, and consistently and reliably and deterministically, whereas other types eg. C#'s "using" statement requires explicit usage, which isn't as convenient at all.
- pjmlp 6y agoOnly if the objects aren't heap allocated via new/delete, and you happen to forget to call delete on it.
- secondcoming 6y agoWell yes, C++'s destructor behaviour doesn't work if you don't use C++...
- pjmlp 6y agoWhich is what plenty of people do across the industry with C++ compilers, they code like C.
- secondcoming 6y agoWell that's not C++'s fault. Those people are either stuck on a particular compiler version, are in a maintenance mode, or are just unwilling to improve themselves. Maybe there's job security in being able to write code in a deprecated style.
- pjmlp 6y agoLike Android NDK?
- secondcoming 6y agoNo idea, I've no experience with it.
- pjmlp 6y agoYou should, then you will see how much C++ it actually uses. It exposes everything as C APIs, even thought it is a mix of Java and C++ on the implementation side, leaving the C++ developers to write their own type safe abstractions from scratch. Most just can't be bothered and write C in C++. Then there is the whole issue how, following Google C++'s guidelines, the NDK is actually implemented.
- unishark 6y agoHistorically there was a big performance hit for using the fancy added abstractions of C++. Don't know if it's still that bad. But in the past it cost a factor of two or so, whenever I tried to get fancy.
- jcelerier 6y ago> Historically there was a big performance hit for using the fancy added abstractions of C++. Don't know if it's still that bad. But in the past it cost a factor of two or so, whenever I tried to get fancy. I don't know how much back in the past it is, but pretty much every time in the last ten years I tried to beat a C++ abstraction, I almost never managed to substantially improve (and since gcc.godbolt.org exists it made it much easier to witness that most of the abstraction layers entirely disappear at -O2)
- ncmncm 6y agoHistorically, meaning "in the '90s". It has been quite a long time since the '90s. Many readers here weren't even born yet when C++ abstraction overhead was last a thing.
- badsectoracula 6y ago> or are just unwilling to improve themselves Or they may not see that stuff as an improvement in the first place (e.g. see Orthodox C++[0]). [0] https://gist.github.com/bkaradzic/2e39896bc7d8c34e042b https://gist.github.com/bkaradzic/2e39896bc7d8c34e042b
- hyperpl 6y agoWow, I thought I was alone.
- ncmncm 6y agoIt is always the right time to learn modern C++. Discomfort is not a valid reason to avoid learning new things.
- Jtsummers 6y agoIf I'm reading that right, they start off talking about a minimalist/reduced subset of C++, and then end up concluding: Use C++14 (now two standards ago) and we'll probably recommend C++17 in 2022 (5 year delay). That seems like a rather weak conclusion given all the words that preceded it. You might as well just start and end with this: New standards have issues, people don't know how to use them correctly, and compilers are slow to catch up. Wait 5 years to use a standard so all these things are worked out, and you don't have to worry about the details that don't stick.
- badsectoracula 6y agoThe C++14 part seem to be added lately and indeed sounds contradictory to what was written previously. As a comment below writes though they use the original Orthodox C++ idea without the C++14 part (and TBH i think the whole "committee" part is a tongue-in-cheek joke). In any case this is just an example, there are a couple of similar "subsets" with the same idea of keeping C++ use to its bare minimum, it is just that i remember this one because of the name :-P
- cgrealy 6y agoThat's not really fair. Thing* thing = new Thing(); thing->DoStuff(); delete thing; is perfectly valid C++ and has been since the invention of the language. It's not a good idea, but it is valid. If, OTOH, you do Thing* thing=(Thing*)malloc(sizeof(Thing)); free(thing); the you fully deserve the inevitable world of pain that is coming your way
- jcelerier 6y ago> is perfectly valid C++ we're in 2020. $ clang-tidy -checks='cppcoreguidelines-*' foo.cpp /tmp/foo.cpp:10:3: warning: initializing non-owner 'Thing *' with a newly created 'gsl::owner<>' [cppcoreguidelines-owning-memory] Thing* thing = new Thing(); ^ /tmp/foo.cpp:12:3: warning: deleting a pointer through a type that is not marked 'gsl::owner<>'; consider using a smart pointer instead [cppcoreguidelines-owning-memory] delete thing; ^ /tmp/foo.cpp:10:3: note: variable declared here Thing* thing = new Thing(); ^
- cgrealy 6y agoDoes it compile? Does it run? Yes, therefore it's valid C++. I'm not arguing it's good C++, but saying "C++'s destructor behaviour doesn't work if you don't use C++" in relation to using new and delete is clearly nonsense.
- umanwizard 6y agoThe malloc/free version is perfectly valid C++ too, isn’t it?
- cgrealy 6y agoEh... kind of? I guess it depends on your definition of "valid". Let's say we have: class Thing { public: Thing() { cout << "created" << endl; } ~Thing() { cout << "destroyed" << endl; } }; Assuming the happy path, the new/delete version will be functionally identical to a more correct/idiomatic { auto thing = make_unique<Thing>(); } The malloc/free version won't call the destructor, and therefore doesn't do what a "normal" object would do. It's splitting hairs, I guess, but that's where I'd draw the line.
- steerablesafe 6y agosmart pointers exist
- pjmlp 6y agoJust like value types and deterministic resource management in GC languages.
- platinumrad 6y ago`new` and `delete` have been banned outside of deep library code in every industrial C++ codebase I've ever worked on.
- pjmlp 6y agoHow industrial do you consider Android? https://android.googlesource.com/platform/frameworks/base/+/refs/heads/master/cmds/statsd/src/external/StatsPullerManager.cpp https://android.googlesource.com/platform/frameworks/base/+/...
- darkmx0z 6y agodid you check the header of that cpp? they are storing it in a shared_ptr: shared_ptr = referenced counted pointer = RAII they should have used make_shared, but it is still safe
- pjmlp 6y agoWhich isn't exception safe, as any good C++ developer would know.
- msla 6y ago"Google Does Bad Thing" is not quite equivalent to "C++ Is Bad Language"
- pjmlp 6y agoIndeed, it is even one of my favourite languages, but contradicts the OP's point of every industry C++ codebase has banned new and delete.
- int_19h 6y agoThe Google C++ style guide alone is sufficient evidence to conclude that Google doesn't know how to do C++ properly.
- 6y ago