2 ms·
> Do you consider D to be a derivative of C++? I don't. They have a near perfect C++ interface. D can even catch C++ exceptions. It's not a near-perfect C++ in
by pcwalton 11y ago
> Do you consider D to be a derivative of C++? I don't. They have a near perfect C++ interface. D can even catch C++ exceptions.
It's not a near-perfect C++ interface, because D can't instantiate C++ templates on its own.
> First of all, C++ cannot be obsoleted by rust, it's economically impossible.
Rust can make C++ obsolete in a technical sense. But I agree of course that C++ is immortal.
> For example, it's trivial to write a low overhead pointer that can't dangle, throws an exception on nullptr deference, and that safely points to an object it doesn't own (even if that object is its owner)
Wrong. You can't write a pointer that can't dangle in C++. Think iterator invalidation, "this" invalidation, etc. I've given innumerable code samples to prove this in the past.
It's hard to argue about this abstractly, so let's do this. If you give me the C++ code for a smart pointer that you claim is safe, I will construct you a code example showing use-after-free using that pointer.
> From what I've observed, you can't do this in Rust, if you try to write a doubly linked list you either have to use C-pointers that can dangle inside unsafe blocks, or eat the Rc overhead.
Yes, you can't do this in Rust. You also can't do it in C++.
C++ is good at making you think that you're using pointers that can't dangle, as evidenced by posts like yours. It's not good at actually preventing things like use-after-free.
> - template meta-programming feels like a different language, it's dynamic, functional, and the difference is refreshing. I like the mental switch you have to do to go from writing a C++ template to writing a C++ model.
I much prefer the static approach of Rust's generics to the untyped templates of C++--the fact that templates feel like a different language is a disadvantage in my view--but this is a huge tradeoff and it largely comes down to taste. This is unlike the memory safety guarantees of Rust pointers, which are not a matter of taste; safety is a formal property that C++ references simply do not satisfy and cannot satisfy no matter what.
- autoreleasepool 11y agoHere's an implementation of <experimental/observer_ptr> set for C++17 aka the world's dumbest (and most poorly named) smart pointer. https://github.com/martinmoene/observer-ptr/blob/master/include/nonstd/observer_ptr.h https://github.com/martinmoene/observer-ptr/blob/master/incl... I'm using a modified version in a personal library. It throws an exception on a nullptr dereference. I'm not sharing that version here because A. it's not ready, and B. You're definitely a better programmer than me and I don't feel like getting totally shit on today :) I thought the whole point of the "world dumbest smart pointer" was that it's automatically nullptr'd for you so it can't dangle. I'm looking forward to hear why this isn't true. I've actually been brooding over this perceived advantage of C++ for a while now.
- pcwalton 11y agostd::vector<std::string> strings; strings.push_back("Hello"); std::observer_ptr<std::string> p(&strings[0]); strings.clear(); std::cout << p; // use after free
- autoreleasepool 11y agoThat wouldn't compile. To use the header I linked it should be `nonstd::observer_ptr<std::string> p(&strings[0]);` You also need to dereference p to use the stream operator. I'm disappointed you presented such a contrived example. You also deliberately avoid const-correctness. That's a little insidious. You need to const everything manually in C++, so as a good C++ programmer you should const everything as you write C++. Only carefully omit const when you need mutability. We all know C++98 is dangerously unsafe. Let's write your example in C++11. #include <iostream> #include <string> #include <vector> #include <memory> #include <utility> #include "observer_ptr.h" // kill the noise using std::cout; using std::vector; using std::string; using nonstd::observer_ptr; int main() { vector<string const> const strings { "Hello" }; observer_ptr<string const> const p(&strings[0]); strings.clear(); // not gonna happen cout << *p; } » clang++ -std=c++11 immutable.cpp immutable.cpp:11:5: error: member function 'clear' not viable: 'this' argument has type 'const std::vector<const std::string>' (aka 'const vector<const basic_string<char, char_traits<char>, allocator<char> > >'), but function is not marked const strings.clear(); // not gonna happen ^~~~~~~ /usr/local/Cellar/llvm/HEAD/bin/../include/c++/v1/vector:735: 10: note: 'clear' declared here void clear() _NOEXCEPT ^ 1 error generated. EDIT: Down voted for legitimate rebuttal. Nice.
- oldmanjay 11y agoIt seems you're sort of missing a big point here. You demonstrate how, with sufficient vigilance, you can write safe C++. The goal of Rust is to move the vigilance into the language and make the unsafety opt-in rather than attempt-to-run-from.