Search code examples
c++templatesscopefriend

Template + Friend (a deadly combination)


Possible Duplicate:
Making an undefined class as friend, and defining it later.

I have the following code

template<typename T> 
class A
{
        class B;
        B b; 
};

int main() 
{
        return 0;       
}

The code doesn't emit any error because A is not instantiated. Am I right?

But have a look at the second code sample

template<typename T>
class A
{
  protected:
        class a 
        {
            int x;
            int y;
          private:
            friend class b;  
        };
        template <typename U > class b
        {  
          int z;
          U y;
        };

};

int main()
{
    A<int>  a;
    return 0;
}

In this case A is instantiated but b is not. So the code shouldn't emit any error, right? But I get a redeclaration error in msvc++2008 professional edition. It compiles fine on gcc.

What could be the reason for the error?

Sorry everyone. After reading the comments and after seeing my original code I see that I made a mistake while typing the second code sample. I missed the definition of the constructor and the statement a *object = new a; inside the constructor. Apologies. That means my code is exactly similar to the other code posted by the other user. But now I cannot delete my question now because of the existing answers.


Solution

  • Regarding you second code snippet ...

    template<typename T>
    class A
    {
      protected:
            class a 
            {
                int x;
                int y;
              private:
                friend class b;  
            };
            template <typename U > class b
            {  
              int z;
              U y;
            };
    
    };
    
    int main()
    {
        A<int>  a;
        return 0;
    }
    

    ... it is most probably invalid code, because a very similar code snippet posted by you earlier, in the SO question "Making an undefined class as friend, and defining it later", is invalid. But I'm not sure about this.

    The difference wrt. your earlier question is only that in that other question the nested class a is used, hence, instantiated.

    However, the redeclaration error that you get with MSVC 8.0 does not necessarily mean that that compiler recognizes the code as invalid standard C++. It might be a compiler bug that just by happenchance causes it to (probably) correctly identify the code as invalid. And it might just be that the above code, not using a, is valid…

    So, it's pretty subtle, it's language lawyer stuff.

    Best advice is to just stay well clear of these rather dark corners of the language.

    EDIT: the earlier question was not Pavel's (but it's the same code); see comments.

    Cheers & hth.,