Making an undefined class as friend, and defining

2019-02-17 04:04发布

Making an unknown friend

template<typename T>
class List
{
protected:

    class a {
        int x;
        int y;
    private:
        friend class b;  // <------------ Why this is not an error? 
    };

    template <typename U > class b {  //If that is not a error this should be an error
        int z;
        U y;
    };

    public:
        List() {
            a* ptr = (a *)new unsigned char[sizeof(a)];
        }
};

int main() {
    List<int>  mylist;
}

Please go through this link, I have my questions as comments in the code. I am trying to make another class a friend of my class. But that class is not know at the time of making friend. What is the C++ rule that allows for it. Later I am defining that class in such a way that, it is incompatible with the friend declaration. Why is that not throwing an error. Thanks

3条回答
放我归山
2楼-- · 2019-02-17 04:26

//Run this- it now will compile for you

template <typename U > class b; //<----- forward declaration

template<typename T>
class List
{
protected:


        class a {
        int x;
        int y;
        private:
          friend class b<T>;  // <------------ Add <T>
        };
        template <typename U > class b { 
          int z;
          U y;
        };

        public:
        List() {
          a* ptr = (a *)new unsigned char[sizeof(a)];
        }
};

int main() {
    List<int>  mylist;

}
查看更多
别忘想泡老子
3楼-- · 2019-02-17 04:33

Yes your code is invalid! This is an interesting show of how templates can change meaning of code in subtle ways. The following code is valid:

class List
{
public:
    class a {
        typedef int type;
        friend class b; // that's fine!
    };

    template <typename U > class b;
};

class b {
  List::a::type an_int; // allowed to access private member
};

Standard says at 7.3.1.2/3

If a friend declaration in a non-local class first declares a class or function83) the friend class or function is a member of the innermost enclosing namespace.

When is it a "first declared class"? It says that too there

When looking for a prior declaration of a class or a function declared as a friend, and when the name of the friend class or function is neither a qualified name nor a template-id, scopes outside the innermost enclosing namespace scope are not considered.

The lookup for "class b" is delegated from 7.1.5.3/2 to 3.4.4 which in turn delegates to unqualified name lookup at 3.4/7. All the question now is whether the template-name "b" is visible in the friend declaration class a. If it isn't, the name is not found and the friend declaration will refer to a new declared class at global scope. 3.3.6/1 about the scope of it says

The potential scope of a name declared in a class consists not only of the declarative region following the name’s declarator, but also of all function bodies, default arguments, and constructor ctor- initializers in that class (including such things in nested classes).

Ignoring a few pedantic points that would make this wording not apply to here (which were a defect but are fixed in the C++0x version of that paragraph which also makes this easier to read), this list does not include the friend declaration as an area where that template name is visible.

However, the friend was declared in a member class of a class template. When the member class is instantiated different lookup applies - the lookup for friend names declared in a class template! The Standard says

Friend classes or functions can be declared within a class template. When a template is instantiated, the names of its friends are treated as if the specialization had been explicitly declared at its point of instantiation.

So the following code is invalid:

template<typename T>
class List
{
public:
    class a {
        typedef int type;
        friend class b; // that's fine!
    };

    template <typename U > class b;
};

// POI
List<int>::a x; 

When that causes List<int>::a to be implicitly instantiated, the name a is looked up at "// POI" as if there would have been an explicit specialization declared. In that case, the template List::b has already been declared, and this lookup will hit it and emit an error because it's a template and not a non-template class.

查看更多
萌系小妹纸
4楼-- · 2019-02-17 04:33

The code is ill-formed and Comeau rejects it giving the following error

error: invalid redeclaration of type name "b" (declared at
      line 11)

I think this is a bug in g++. Intel C++ rejects it too. You can fix the code by defining class B above A.

template <typename U > class b { 
        int z;
        U y;
};
class a {
        int x;
        int y;
    private:
        friend class b<T>;  
};
查看更多
登录 后发表回答