开发者

Template + Friend (a deadly combination) [duplicate]

This question already has answers here: Closed 12 years ago.

Possible Duplic开发者_运维问答ate:

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.


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.,


Class a doesn't has an access to the members of the enclosing class according to the current Standard §11.8. But the Standard has a defect report to this. So some compilers works according to the original Standard, and the others according to the proposed resolution of the defect report.


MSVC requires the full template decl for friend template classes, MSDN has an example here, specifically the bottom example:

template <class T>
class X
{
private:
   T* data;
   void InitData(int seed) { data = new T(seed); }
public:
   void print() { cout << *data << endl; }
   template <class U> friend class Factory;
};

template <class U>
class Factory
{
public:
   U* GetNewObject(int seed)
   {
      U* pu = new U;
      pu->InitData(seed);
      return pu;
   }
}

so yours should declare:

template <typename U > friend class b;


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

I am not sure, but I don't think so. A compiler which emits an error is also equally standards conformant as a compiler which does not.

The below quote along with the example that immediately follows is a proof of this to my mind

$14.6/7 - "[Note: if a template is instantiated, errors will be diagnosed according to the other rules in this Standard. Exactly when these errors are diagnosed is a quality of implementation issue. ]"

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜