To help LDC/GDC

Walter Bright newshound2 at digitalmars.com
Wed Apr 10 11:49:11 PDT 2013


On 4/9/2013 12:52 AM, Manu wrote:
> But that's meaningless though, because there's always the possibility that
> something somewhere else may have a non-const reference to that thing.
> Can you suggest a case where const could EVER be used in any sort of optimisation?

Sure:

    pure int bar(const int*);

    int foo(int* pt) {
        int t = *pt;   // (1)
        int x = bar(&t);
        return t +       // guaranteed to be same value as (1)
             x;
    }

But the main purpose of const is so that you can have a single function that can 
operate on both mutable and immutable references. Otherwise, you'd have to do 
the copy/pasta two-step and write two functions.


More information about the Digitalmars-d mailing list