lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


On Fri, Aug 2, 2013 at 9:44 AM, Mark Hamburg <mhamburg.ml@gmail.com> wrote:
> Regarding C++...
>
> Value-based semantics (e.g., std::vector) encourage the attitude that objects can be copied as easily as integers. But unless your value actually is implemented using COW this isn't the case. Move semantics in C++ 11 help reduce the number of cases where a copy actually gets made but they also add another level of complexity to the language.

C'est la vie. This is a case where I'm happy to make the complexity
tradeoff -- C++11 is really starting to feel like a modern language
now and I've found coding in it to be a pleasure.

> And if you are implementing COW, now you need to go back and figure out how to manage that. The answer is probably a shared (reference counted) pointer.

Yes, refcounted pointer with detachable semantics is inevitably the
solution, but fortunately there's not actually a problem with circular
references here, as you can't conceptually store a value-semantic
object inside itself.

> If you just use references, you need to be aware that they are potentially only valid for a limited period of time. If you take the address of an object, that's basically a pointer just waiting to go bad.
> Try using an object from multiple threads in C++ and figure out when it is safe to dispose of it without reference counting (or something like it).

That's why you don't use C++ references if lifecycle management is an
issue; you use weak references that zero themselves when the referent
is deleted.

> My general point about C++ is that it provides tools that can be used to provide precise and reasonably efficient memory management but it also provides plenty of ways to overuse memory and to create unsafe situations and neither of the latter necessarily looks like bad code on the surface. (cf the remark that the problem with LISP is that efficient code and inefficient code look the same)

I'll grant you that, although a well-designed library will insulate
you from the problem.

/s/ Adam