What's wrong with just a runtime-checked const?

xs0 xs0 at xs0.com
Fri Jul 21 04:59:38 PDT 2006


> Ok, I'm not following you guys. Can you explain from scratch what it is 
> you were trying to achieve (the purpose of the function?), which when 
> done with static const checking causes unnecessary dups?

It's really simple. Say you have a function that sometimes modifies its 
data and sometimes doesn't. The non-const version is simple, as it's 
allowed in-place modification:

byte[] filter(byte[] data)
{
     if (...)
         data[0] = 10;
     return data;
}

Now, whenever you have const data, you can only use it by duping:

const byte[] data = foo();
byte[] filtered = filter(data.dup);

That's bad, because you always have to .dup the data, even if filter() 
does nothing with it. So, you also write a const version:

const byte[] filter(const byte[] data)
{
     if (...) {
         byte[] result = data.dup;
         result[0] = 10;
         return result;
     }
     return data;
}

That's much better, because a copy is only made when needed. However, 
there's still a problem - the return type must be const, because you 
need to be able to return the original (const) parameter. Therefore, the 
information about whether a copy is made is lost.

With a single function that's not even a problem, but say your filtering 
is configurable and you call 20 filters. If you use the const versions, 
20 copies will be made, even though only one is necessary. If you use 
the non-const versions, you're forced to dup before the first filter,
even when not necessary at all.

A similar problem occurs in this case:

const char[] handleDesc()
{
     if (_handle) {
         return format("Handle #%d", _handle.id());
     } else {
         return "No handle";
     }
}

Because you sometimes return a literal, you must declare the result 
const, even though you'll return a fresh writable char[] most of the 
time, which may or may not be significant. Alternatively, you can .dup 
the literal before returning it, but it will usually be a wasted .dup..

In both cases, the problem is the same - there is no way for the 
function to return information about whether a copy was made along with 
the data itself. It can't even neatly be solved by wrapping the thing in 
a struct or checking equality of references, because the type in either 
case is static and one way or another, you'll have to work around that 
(by casting or whatever)


xs0



More information about the Digitalmars-d-learn mailing list