模板+朋友(致命组合) [英] Template + Friend (a deadly combination)

查看:78
本文介绍了模板+朋友(致命组合)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能重复:
将未定义的类设为好友,并在以后定义它.

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

我有以下代码

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

int main() 
{
        return 0;       
}

由于未实例化A,所以代码不会发出任何错误.我说的对吗?

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;
}

在这种情况下,实例化了A,但没有实例化b.所以代码不应该发出任何错误,对不对?但是我在msvc ++ 2008专业版中收到重新声明错误.在gcc上可以正常编译.

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?

对不起,大家.阅读注释并查看原始代码后,我发现输入第二个代码示例时犯了一个错误. 我错过了构造函数的定义和a * object = new a;语句.在构造函数中.道歉.这意味着我的代码与其他用户发布的其他代码完全相似.但是由于现有的答案,现在我不能删除我的问题.

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;
}

...这是最有可能的无效代码,因为您之前在SO问题无效.但是我不确定.

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

区别wrt.您先前的问题只是在另一个问题中,使用嵌套类a进行实例化.

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

但是,您使用MSVC 8.0遇到的重新声明错误并不一定意味着编译器将代码识别为无效的标准C ++.这可能是一个编译器错误,仅由于偶然的原因导致(可能)正确地将代码标识为无效.而且可能只是上面的代码(不使用a)是有效的…

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 :先前的问题不是Pavel提出的(但是是相同的代码);查看评论.

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

干杯hth.,

这篇关于模板+朋友(致命组合)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆