VS2015 Update 1错误或C ++错误:为什么朋友类无法访问其朋友的受保护析构函数? [英] VS2015 Update 1 bug, or bad C++: Why can't a friend class access its friend's protected destructor?

查看:49
本文介绍了VS2015 Update 1错误或C ++错误:为什么朋友类无法访问其朋友的受保护析构函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下内容似乎是ZeroC ICE在其自动生成的代码中采用的模式,在我看来,这是他们现在为许多版本的工具制作单例(不确定原因)的一种方式.直到今天我发现Visual Studio 2015 Update 1(VS版本14.0.24720.00,VC ++版本19.00.23506)发出错误之前,各种编译器对此都没有问题.在Update 1之前,VS2015也没有问题.我不确定是2015年更新版VS2015 C ++编译器中的错误(回归?),还是其他编译器允许滑动的错误(不符合标准的)C ++代码.

The following appears to be a pattern employed by ZeroC ICE in the code it auto-generates, which appears to me to be a way they have made singletons (not sure why) for many releases of their tool now. Various compilers have no problem with it, until I found today that Visual Studio 2015 Update 1 (VS version 14.0.24720.00, VC++ version 19.00.23506) emits an error. Before Update 1, VS2015 also had no problem with it. I'm not sure whether it's a bug (regression?) in the VS2015 C++ compiler with Update 1, or bad (not standards-conformant) C++ code that other compilers let slide.

以下是代码模式的示例:

Here is an example of the code pattern:

class Foo {
protected:
    virtual ~Foo() {}

    friend class Foo_init;
};

class Foo_init {
public:
    Foo init;
};

static Foo_init staticFooInit;

VS2015 Update 1发出以下错误:

VS2015 Update 1 emits these errors:

example.cpp(13): error C2248: 'Foo::~Foo': cannot access protected member declared in class 'Foo'
example.cpp(3): note: see declaration of 'Foo::~Foo'
example.cpp(1): note: see declaration of 'Foo'

我找到了一个(尚未答复)

I found one (as yet unanswered) ZeroC ICE forum post which seems related to this, but otherwise I haven't found out in my Google searching anything that convinces me whether this is a compiler issue or bad code. I admit I don't know ZeroC ICE very well, nor do I use C++ friend classes enough to have a deep understanding of what you can and can't do with them. I'm hoping someone more knowledgeable can shed some light on it.

推荐答案

对于您的确切问题,我不是100%肯定的,但这使我想起了我前一段时间遇到的一个问题,即前向声明的类将具有意外的范围.此页面 cppreference类突出显示了规则,即向前声明的类具有大多数本地范围.但是,您在我的VS2015u3上的示例也不会失败.

I am not 100% sure on your exact problem, but it reminds me of a problem I had a while ago, where forward declared classes would have an unexpected scope. this page cppreference class highlights the rules, that a forward-declared class has the most local scope. However, your example on my VS2015u3 does not fail either.

我认为解决方法可能是向前声明该类,该类是该类之前的朋友,以便它具有明确定义的范围.

I think the fix is probably to forward declare the class which is a friend before the class, so that it has a well defined scope.

当您有这样的课程时,例如

When you have a class such as

class Example {
     int someFunction( class SomeOtherClass & param );
};

编译器处理本地范围内的 SomeOtherClass 声明.

The compiler treats declaration of SomeOtherClass which is within the local scope.

这意味着

class Example {
     int someFunction( class SomeOtherClass & param );
};

class SomeOtherClass {
          ...
};

声明三个类 Example Example :: SomeOtherClass SomeOtherClass

将示例更改为

class Foo_init;

class Foo {
  protected:
    virtual ~Foo() {}

    friend Foo_init;
 };

class Foo_init {
  public:
    Foo init;
 };

 static Foo_init staticFooInit;

应该工作

这篇关于VS2015 Update 1错误或C ++错误:为什么朋友类无法访问其朋友的受保护析构函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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