What's wrong with just a runtime-checked const?
Bruno Medeiros
brunodomedeirosATgmail at SPAM.com
Tue Jul 18 06:46:14 PDT 2006
Reiner Pope wrote:
>>> In my view, we either need runtime-checked const or nothing. Consider
>>> this, and tell me how we can avoid excess dup calls if we use
>>> C++-style const-checking:
>>>
>>> class foo {
>>> private char[] _name;
>>> public const char[] getName()
>>> {
>>> return _name;
>>> }
>>> ...
>>> }
>>> ...
>>> Import std.string;
>>> foo f;
>>> /*const*/ char[] name = f.getName();
>>> char[] name_m = tolower(name);
>>> // Can we modify name_m? Better dup it, just to make sure
>>> name_m = name_m.dup();
>>
>> if (name_m is name)
>>
>
> Yes, but try getting that to work with a statically-checked const. It
> simply won't work unless you have a very smart checker. Of course, you
> could leave const out altogether, but then we would be making no
> progress. Let me demonstrate the static const-checking problem:
>
> char[] tolower(const char[] input) // the input must be const, because
> we agree with CoW, so we won't change it
> {
> // do some stuff
> if ( a write is necessary )
> { // copy it into another variable, since we can't change input (it's
> const)
> }
> return something; // This something could possibly be input, so it
> also needs to be declared const. So we go back and make the return value
> of the function also a const.
> }
>
> // Now, since the return value is const, we *must* dup it.
>
> Can you suggest another solution other than avoiding const-checking
> entirely? By the way, a dedicated string class is actually an
> implementation of runtime const checking.
>> but, it's certainly not a pretty solution... and there's a related
>> question - how can tolower know whether to .dup or not.. there are
>> certainly cases where it isn't necessary.
> This is just another issue that static checking can't solve but runtime
> checking can (providing we have a modification of the libraries to
> support a CoW and a in-place version).
I'm not following you. That code example shows how a static const
checking should work, so what is the problem with that?
And in:
char[] name_m = tolower(name);
// Can we modify name_m? Better dup it, just to make sure
You ask if we can modify name_m, the answer is yes if the return type of
tolower is non-const, no if it const, so what's the issue here?
--
Bruno Medeiros - CS/E student
http://www.prowiki.org/wiki4d/wiki.cgi?BrunoMedeiros#D
More information about the Digitalmars-d-learn
mailing list