jeudi 2 juillet 2015

non-static data member initialization from mother protected field

I have an issue trying to use non-static data member init. in a complex template inheritance chain. I attach a small non-working example:

struct Builder {
    template <typename T> T& get() {
        return a;
    };

    float a = 5;
};

struct Base {
    Builder a;
};

template <typename T> struct A: public Base {};

template <typename T> struct B: public A<T> {

    float& b = (A<T>::a).get<float>(); // Do not work

    Builder& builder = A<T>::a;
    float& c = builder.get<float>(); // Work
};

struct C: public A<float> {

    float& b = a.get<float>(); // Work
};

int main() {
    return 0;
}

I'm most interested in class B. I have a compilation error using gcc 4.9.2:

error: expected primary-expression before ‘float’
     float& b = (A<T>::a).get<float>(); // Do not work

I don't understand why it does not work as ti does compile if I use the trick two lines below (suffixed by the comment Work) which is basically the same thing.

I also work about of the box if my class is not a template. In this case, I can directly access the protected field by its name without using the syntaxe ParentClass<T>::field.

Do you have any ideas of what I'm doing wrong here?

Thanks a lot for your help!

Aucun commentaire:

Enregistrer un commentaire