简体   繁体   中英

Access private member of nested friend class in specialized template

The following code fails to compile in Visual studio 2103 Express preview:

template<int N> class TTOuter;

template<>
class TTOuter<1>
{
public:
    class inner
    {
        friend class TTOuter<1>;

    private:
        inner(int n) : data(n) {;}
        int data;
    };

private:
    inner x;

public:
    TTOuter(int n) : x(n) {;} //Fails here
};

Error: "TTOuter<1>::inner::inner(int n)" is inaccessible

An analogous access succeeds if the outer class is not a specialized template:

class Outer
{
public:
    class inner
    {
        friend class Outer;

    private:
        inner(int n) : data(n) { ; }
        int data;
    };

private:
    inner x;

public:
    Outer(int n) : x(n) { ; }
};

Gives no error.

I tried forward declaring TTOuter<1> like:

template<> class TTOuter<1>;

I also tried replacing the friend declaration by:

template<int N> friend class TTOuter;

But neither works.

Would appreciate any insight. Thank You.

I think it is nothing more than a typo in your code

public:
    TOuter(int n) : x(n) {;} //Fails here
  //^^^^^^ Shouldn't it be TTouter???
};

Edited:

If I edit a bit your code:

class TTOuter<1>
{
public:
    typedef TTOuter this_type; // <-- I add this typedef
    class inner
    {
        friend class this_type; // <-- Intellisense works
        //friend class TTOuter<1>; // <-- intellisense barks
        inner(int n) : data(n) { ; }
        int data;
    };
    TTOuter(int n) : x(0) {}
};

Now intellisense stops complaining.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM