Another compile-time perversion. Or not?
Don Clugston
dac at nospam.com.au
Wed Sep 6 00:45:17 PDT 2006
Bruno Medeiros wrote:
> Walter Bright wrote:
>> Don Clugston wrote:
>>> Walter Bright wrote:
>>>> There's a bit of chicken and egg problem with it - can't tell if
>>>> it's referenced or not until code generation, which is intimately
>>>> wrapped up with object file generation.
>>>
>>> But isn't it true that a template consisting only of
>>> 'const' and 'static if' will *never* result in code generation?
>>
>> There can be:
>>
>> template Foo(T)
>> {
>> static if (...)
>> const T x;
>> }
>>
>> int* p = &Foo!(int).x;
>>
>
> Yes, because there are two very different meanings for "const" in D :
> there's "compile-time constant", and there's "variable that can only be
> assigned once", for which the keyword 'final' is used in every other
> modern C-like language, which is how it should be done in D as well (or
> with some other keyword).
Bruno, I agree that 'final' would be much better for 'write-once'
values; but the compiler can tell the difference anyway, based on
whether it has an =.
If you change the line to
const T x = 6;
then the problematic line won't compile. By the time the compiler has
finished instantiating the template, it should know if it contained
'const=' or 'write-once const'.
I really think that it is worth putting some thought into making this
early discarding possible. If all else fails, one possibility would be
to allow 'const' in front of 'template' to signify that the template
should be discarded. But I don't think that's necessary.
The refined rule is:
Templates with only 'const =' and 'static if' can always be discarded.
More information about the Digitalmars-d-announce
mailing list