Variable lookup in derived local class of template function

26 views Asked by At

I'm experimenting with local classes in C++ and stuck with following code:

void f1(int a)
{
    struct Inner1
    {
        int a;
    };

    struct Inner2 : Inner1
    {
        void foo()
        {
            a = 10; // Okay
        }
    };
};

template<typename T>
void f2(T a)
{
    struct Inner1
    {
        T a;
    };

    struct Inner2 : Inner1
    {
        void foo()
        {
            a = T(10); // error: use of parameter from containing function
        }
    };
};

template<typename T>
void f3(T a)
{
    struct Inner
    {
        T a;

        void foo()
        {
            a = T(10); // Okay
        }
    };
};

int main()
{
    f1(10);
    f2(10);
    f3(10);
    return 0;
}

Why is it so? Am I right, it's a compiler error (I'm using gcc 4.8.4 and gcc 4.9.2) and code is correct?

0

There are 0 answers