this(this) must be cheap and O(1)
Jonathan M Davis
jmdavisProg at gmx.com
Sat Sep 24 04:58:39 PDT 2011
On Saturday, September 24, 2011 07:30:56 Trass3r wrote:
> > This means that objects with large state would need to use things like
> > COW and/or reference counting.
>
> Isn't an expensive-to-copy type supposed to be a class anyway?
That would be one of the arguments for insisting that struct copying be cheap.
But in C++ (where there is no significant difference between classes and
structs), you can put a class on the stack and it is _not_ necessarily cheap
to copy. Andrei is suggesting that we _not_ follow that, but rather that we
assume that structs can be copied in O(1), which means that user-defined types
which would be expensive to copy either need to use stuff like reference
counting and COW, or they need to be classes. But unless we make such an
assumption/requirement, then there isn't necessarily an expectation that
expensive-to-copy types would be classes.
> > I'd go as far as requiring this(this) to be nothrow, but perhaps it
> > would be best to see whether that is a necessity.
>
> Don't memory allocations prevent a function from being nothrow?
> Then this would make it impossible to properly wrap an array in a struct.
nothrow guarantees that no Throwables derived from Exception are thrown from a
function. OutOfMemoryError is an Error, and Error is _not_ derived from
Exception. Errors are expected to be non-recoverable and aren't really meant
to be caught.
So, nothrow has _no_ effect on memory allocations. If it did, nothrow would be
pretty useless.
- Jonathan M Davis
More information about the Digitalmars-d
mailing list