const assignments problem again

bearophile bearophileHUGS at lycos.com
Sat Aug 20 15:11:18 PDT 2011


Kagamin:

> P.S. you would make a better point if it were a const instance of a class.

You can't even define V as "const struct" and use it like this:


const struct V { double x, y; } // wrong
void main() {
    double x = 1, y = 2;
    int c = 1;
    V v;
    switch (c) {
        case 1:
            v = V(x, y);
            break;
        case 2:
            v = V(-x, -y);
            break;
        // other cases here
        default: assert(0);
    }
}


This works (the asm shows DMD doesn't inline the function):

const struct V { double x, y; }
void main() {
    double x = 1, y = 2;
    int c = 1;
    V v = {
        switch (c) {
            case 1:  return V(x, y);
            case 2:  return V(-x, -y);
            // other cases here
            default: assert(0);
        }
    }();
}

Bye,
bearophile


More information about the Digitalmars-d mailing list