dimanche 24 juillet 2016

Accessing a inherited static member without specifying the scope

Followup to: Enabling a static member conditionally without changing the member scope

I have got a static class member which is conditionally enabled when both template variables R and C are equal.

Now I want to access it without having to specify the scope beforehand - just like with any direct static member.

One can pull variables into the derived classes scope using a using A::variable; statement. But as the variable is not available for all Foo types, this using declaration will fail when R != C.

struct EmptyBase { };
template<int R, int C> class Foo;

template<int R, int C>
class _Foo_Square
{
public:
    static Foo<R, C> ID;
};

template<int R, int C>
class Foo : public std::conditional<R == C, _Foo_Square<R, C>, EmptyBase>::type
{
public:
    static Foo SECOND;
    /* other members and functions */

    void A()
    {
        Foo a = ID; //error because of inheritance
        Foo b = Foo<R, C>::ID; //works

        Foo test = SECOND; //works everytime
    };
};

int main()
{
    Foo<3, 3> foo;
    foo.A();
}

Accessing ID directly in the member function does not work as expected.

Aucun commentaire:

Enregistrer un commentaire