Am 14.02.2012, 15:46 Uhr, schrieb Timon Gehr <[email protected]>:

It cannot be a compiler optimization because it potentially changes the semantics of the code:

template T(U, V, W){
     @templated(W) W idx;
}

void main(){
     T!(int,double,long).idx = 1;
     assert(T!(double,int,long).idx == 1);
}

Ah right, I was a bit off-topic then: @templated removes duplicate static data (if desired) whereas the code gen optimization removes duplicate code (automatically). Can we make this distinction at least, or did I miss something else? The initial post by bearophile was about both - code and data, and I argue that the compiler can figure out which code/methods are duplicates, which would be preferred over doing it manually.

These uses would remain:

template T(U, V, W) {
    @templated(W) W idx;
}

struct Foo(U, V, W) {
    void bar() { // one method per W
        @templated(W) static W value;
    }
    void foobar() { // one method per U, V, W
        static W value;
    }
}

while this would be invalid:

struct Foo(U, V, W) {
    @templated(W) void bar() {
        static W value;
    }
}

The reasons are that it could duplicate a safe compiler optimization if used on methods and I think @templated is easier to understand if it only applies to static data.

-- Marco

Reply via email to