Top 5

Andrei Alexandrescu SeeWebsiteForEmail at erdani.org
Thu Oct 9 07:12:46 PDT 2008


Don wrote:
> Steven Schveighoffer wrote:
>> "Benji Smith" wrote
>>> Gide Nwawudu wrote:
>>>> 2) Finalise const/invariant stuff and change manifest const from
>>>> 'enum' to 'define' (or whatever).
>>>>
>>>> define {
>>>> double PI = 3.14;
>>>> string author = "Walter";
>>>> }
>>>> define enum Direction { North, South, East, West };
>>> I've never quite understood what people are talking about when they 
>>> refer to a "manifest" constant. What does that mean?
>>>
>>> And why do we need any special keyword? What does the "define" 
>>> keyword give you that an ordinary variable declaration doesn't? Why 
>>> not just write the code from above like this:
>>>
>>>   double PI = 3.14;
>>>   string author = "Walter";
>>>   enum Direction { North, South, East, West };
>>>
>>> What am I missing here?
>>
>> You cannot take the address of a manifest constant, and it doesn't 
>> live in a static data space or in memory anywhere.  Instead it is 
>> built directly into the code.
>>
>> So when you say
>>
>> define double PI = 3.14;
>>
>> And then use PI:
>>
>> auto x = PI;
>>
>> This generates code that declares a variable x, then assigns it to 
>> 3.14. the PI symbol isn't stored in the final code.
>>
>> This has a huge benefit when you are declaring lots of constants, but 
>> only few will be used.  You don't have to pay the penalty of storing 
>> all the constants in your code, only the ones you use, and only where 
>> you use them.
> 
> Actually a smart linker would do that anyway. The only reason we need 
> manifest constants is because OPTLINK isn't smart enough.
> (And DMD isn't smart enough to discard unreachable variables from the 
> symbol table).

Exactly so.

Andrei



More information about the Digitalmars-d mailing list