9 ms·
C++14 (and higher) feels like coding in Go or Python. C++17 is so full of good stuff I'm considering beta compilers just to get it. Soon Python and Ruby won't
by WildUtah 10y ago
C++14 (and higher) feels like coding in Go or Python.
C++17 is so full of good stuff I'm considering beta compilers just to get it. Soon Python and Ruby won't have any advantages left in writing beautiful, clear, correct, bug-free, and easy to read code and C++ will still run 200x faster.
- EamonnMR 10y agoCare to share any resources where you can learn about the new features that take it there?
- WildUtah 10y agoWish I could. The hours I've spent reading the formal committee proposals and specs would have been better spent on video games and junk food. But there will be books, and good ones. Next year probably. The compilers aren't even ready yet.
- VodkaHaze 10y agoThe best I found are CppCon youtube videos, really
- justin66 10y ago> Soon Python and Ruby won't have any advantages left in writing beautiful, clear, correct, bug-free, and easy to read code Come on, it's not like they've added garbage collection.
- corysama 10y agoNo garbage collection. But, modern C++ recommendations pretty much deprecate manual memory management for 90% of situations. std::vector and std::make_unique for 80% of everything; other containers and make_shared when you really have to; malloc and new for specialty situations.
- gowld 10y agoThat's still a lot more work and error-prone than using garbage-collection.
- sqeaky 10y agoit more like 99% of the time and as long you don't go near new or malloc you generally can't leak. Then with RAII you can clean up any kind of resource. Most languages have pretty rudimentary tools for closing files and worse for releasing mutexes. These tasks are almost easy to get right in modern C++.
- WildUtah 10y agoI still love 'with-open-file and its sisters in LISP. Get that resource in one line and it's guaranteed to be cleaned up right without ever thinking about it again. Then you have total freedom to use it however you want and the results destructure themselves.
- civility 10y agoYou could implement something comparable: int main() { with_open_file_for_writing("foo.dat", [](FILE* fp) { fprintf(fp, "old style I/O, automatically closed\n"); }); return 0; }
- Animats 10y ago"With" is even better in Python. Exceptions during closing work right. The big problem with RAII is that if anything goes wrong in a destructor, you're in trouble.
- Cyph0n 10y agoNewbie C++ programmer here. Firstly, what exactly is the difference between make_unique and plain old unique_ptr? Why do I need to include a separate template to use the former? A related issue I faced today. Say I want to create a std::vector of objects. From what I understand, I have three options. 1) Use raw pointers, but be sure to cleanup using delete. 2) Use shared_ptr, which will ensure that your object is safely deleted once no one is referencing it. 3) Create a stack allocated object, then push that onto the vector. The vector will create a shallow copy of the object, and the original copy will be deleted at the end of the current scope. In this scenario, I wouldn't be able to use unique_ptr, because that would mean that both the vector and the local scope have pointers to that object. Am I correct? Are there other options for achieving this that I don't know of? Also, which one is the best approach for the general case? I ended up using shared_ptr and it seems to be working fine... I think.
- WildUtah 10y agoGarbage collection could prove to be a dead end. Rust and Swift are prospering as the latest cool things because they don't have any. Decades from now we may be hearing about garbage collection as a vastly expensive experiment that simply didn't pan out. The clever combination of declarative management for common cases and reference counting for complicated cases looks like the winner in the long run.
- justin66 10y ago> Garbage collection could prove to be a dead end. You'd have to elaborate on that an awful lot to be at all persuasive. > Rust and Swift You were comparing C++ to Python and Ruby. We're talking two different paradigms here.
- deleted 10y ago[deleted]
- pjmlp 10y agoSwift surely has garbage collection. Reference counting is a garbage collection algorithm.
- sqeaky 10y agoIn that case C++ has garbage collection too with std::shared_ptr. I guess 1 is a count too so also with std::unique_ptr.
- WildUtah 10y agoIndeed, according to these "ref counting is gc" folks, free(void *) is a garbage collector, so C has always been garbage collected since 1970.
- pjmlp 10y agoNo, because the language C doesn't provide any of the algorithms described here: http://gchandbook.org http://gchandbook.org Although, there are conservative GCs for C that replace malloc () and make fre () a nop, they cannot release all memory properly due to C semantics.
- clishem 10y agoWho needs garbage collection when you have std::shared_ptr.
- lokedhs 10y agoProbably people who are having more complex data than a single tree. Exaggerated, I know, but it's not like your post was particularly nuanced.
- civility 10y ago> Probably people who are having more complex data than a single tree. Reference counting is safe with a DAG (not just a tree), but I get your point. For the times I've needed an arbitrary graph data structure, I used an adjacency matrix to store it.
- duneroadrunner 10y agostd::shared_ptr works, but fyi often there's a slightly better alternative. For cases where the target object is not shared between asynchronous threads, mse::TRefCountingPointer[1] is smaller/safer/faster/more scalable. For cases where the target is shared between threads (while being modified) mse::TAsyncSharedReadWriteAccessRequester[2] is safer. [1] shameless plug: https://github.com/duneroadrunner/SaferCPlusPlus#reference-counting-pointers https://github.com/duneroadrunner/SaferCPlusPlus#reference-c... [2] https://github.com/duneroadrunner/SaferCPlusPlus#asynchronously-shared-objects https://github.com/duneroadrunner/SaferCPlusPlus#asynchronou...
- Shorel 10y agoPut your data in the stack.
- hellofunk 10y agoIf only the stack were large enough for everything. In which case, use unique_ptr.
- sqeaky 10y agoIsn't not having garbage collection and advantage? I mean we have destructors which handle so much more than garbage memory. Close files, release mutexes, close sockets, cleanup thread pools, manage the lifetime of anything. One time I opened a web browser in the constructor and closed it in the destructor, and I had full confidence that no matter what crap happened in the middle my app never leaked whole browsers.
- thesmallestcat 10y agoMemory fragmentation? And alloc/free aren't free. So now you have to hack together your own memory management scheme and hope it performs well enough in both areas to be worth the extra effort and lost safety.
- matthewaveryusa 10y agoContemporary allocators make allocation cheap. Not sure how GC is any better at fragmentation.
- mrob 10y agoSome garbage collectors can move objects to remove fragmentation and update the pointers to them. eg: https://hacks.mozilla.org/2015/07/compacting-garbage-collection-in-spidermonkey/ https://hacks.mozilla.org/2015/07/compacting-garbage-collect...
- Impossible 10y agoIf you're thinking like that I don't see why you wouldn't prefer Java or C#? One of the major advantages of C and C++ is explicit memory allocation and lack of GC pauses.
- justin66 10y agoA person could wish for a more compact and lightweight runtime than what Java and .Net offer while still having GC. I imagine some of those people are checking out Go nowadays.
- beached_whale 10y agoHerb Sutter presented one in a talk a little while ago. https://github.com/hsutter/gcpp https://github.com/hsutter/gcpp https://www.youtube.com/watch?v=JfmTagWcqoE https://www.youtube.com/watch?v=JfmTagWcqoE So it's totally doable in library without cost to those that do not need it.
- justin66 10y agoThat looks like a great talk, as all of Sutter's talks are. Thanks.
- hellofunk 10y agoWith C++ since C++11, there is little need for garbage collection. I haven't written a manual memory "new" or "delete" in years, thanks to unique_ptr (which in my benchmarks is somehow also faster than manually managing the memory).
- santaclaus 10y ago> C++17 is so full of good stuff Structured bindings are going to be amazing! Yea we didn't get modules, but I for one am psyched about this feature alone. If only they were faster to upgrade compilers at work...
- contravariant 10y ago> Soon Python and Ruby won't have any advantages left in writing beautiful, clear, correct, bug-free, and easy to read code Does that mean C++17 will finally get support for the generator pattern?
- tathougies 10y agoThe generator pattern is really not that fundamental a language construct, and, in general, is quite straightforward to implement in languages with powerful enough abstractions. C++ is one such language: http://www.boost.org/doc/libs/1_62_0/libs/coroutine/doc/html/coroutine/coroutine/asymmetric.html http://www.boost.org/doc/libs/1_62_0/libs/coroutine/doc/html...
- gpderetta 10y agoI wouldn't call it straightforward, boost.coroutine requires custom assembler for its implementation. Then again, stackfull coroutines are significantly more powerful than generators available in most other programming languages (notable recent exceptions are Go and Lua). Boost.asio provides stackless generators [1] implemented purely in C++, by (ab)using the preprocessor and the switch statement. [1] http://www.boost.org/doc/libs/1_62_0/doc/html/boost_asio/overview/core/coroutine.html http://www.boost.org/doc/libs/1_62_0/doc/html/boost_asio/ove...