Top 5

Steven Schveighoffer schveiguy at yahoo.com
Thu Oct 9 06:21:36 PDT 2008


"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.

-Steve 





More information about the Digitalmars-d mailing list