4 ms·
> The lack of transitivity for one. Const pointers shouldn't be able to modify the pointed objects Are they? I either misunderstand or disagree. The syntax get
by fhd2 13y ago
> The lack of transitivity for one. Const pointers shouldn't be able to modify the pointed objects
Are they? I either misunderstand or disagree. The syntax gets a bit silly with pointers, because you can have const pointers and pointers to const (and const pointers to const of course):
const Foo* foo; // Cannot change *foo, but can change the pointer (e.g. foo = 0)
Foo* const foo; // Can change *foo, but cannot change the pointer
const Foo* const foo; // Cannot change *foo or the pointer
> The fact that working with non-const-correct libraries is a nightmare (prepare to const_cast a lot!).
Agreed!
> As far as I know, Common Lisp users have some kind of RAII with macros, but I'm not familiar enough to compare it to C++.
In my experience, the kind of problem RAII is useful for is far less common in Lisps (can only really speak for Clojure). One tends to not encapsulate resource management in any way, so handling it explicitly in the caller is fine. And you can use macros to do something when the scope is excited.
So in Clojure e.g., you can use with-open to do something with a file:
(with-open [writer (io/writer "foo.txt")]
(write writer "Hello")))
with-open ensures that the reader is properly closed when the scope is exited or when an exception occurs.
In C++ (or any OOP language I guess), you'd want something like this:
File foo("foo.txt");
foo.write("Hello");
So I suppose it depends on the philosophy. For OOP languages, RAII makes a lot of sense IMO.