const debacle
Steven Schveighoffer
schveiguy at yahoo.com
Sat Mar 22 08:48:05 PDT 2008
"Janice Caron" wrote
> On 21/03/2008, Walter Bright wrote:
>> There's been some discussion about this, but no good resolution.
>
> Allow me to offer a suggestion.
> ...
Sorry to say this, but this idea adds nothing to templates. Since const T,
invariant T, and just T are all distinct types, I can just write the
template:
T[] f(T)(T[] buf)
And it accomplishes what you are trying to do (instantiates a version based
on the constness of T). If I want to guarantee T is a char, I think I can
do:
T[] f(T : char)(T[] buf)
or something similar (I don't work with D2 daily, so I'm not sure how this
syntax would work), or if this doesn't work, use a static if.
This does not solve the problem that I had originally queried about. The
problem with your method is still that the non-const version does not
guarantee that f does not change the input buffer. I want to specify that
the function does not change the input buffer, and returns the same type as
passed in (without duping the input).
I have been thinking about this a lot, and I think actually in the grand
scheme of things, this is not a blocker, as even though the compiler does
not guarantee constness for the non-const version, one can document that a
function is const (and even test this by compiling a const version in a
unittest). However, it is a hole in the const scheme, which should probably
be fixed at some point.
-Steve
More information about the Digitalmars-d
mailing list