为什么 Visual Studio 2010 调试器看不到静态常量类成员? [英] Why Doesn't the Visual Studio 2010 Debugger See static const Class Members?

查看:73
本文介绍了为什么 Visual Studio 2010 调试器看不到静态常量类成员?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个问题与随后提出的问题密切相关此处.

This question is closely related to a subsequently asked question here.

定义类内常量的方法由 Stroustrup 这里描述.

The method of defining in-class constants is described here by Stroustrup.

当我遵循 Stroustrup 的方法时,我看到了预期的结果.但是,在 Visual Studio 2010 中,调试器无法解析该类范围内的 static const 类成员.这是我的意思:

When I follow Stroustrup's method I see the expected results. However, in Visual Studio 2010, the debugger cannot resolve a static const class member within that class's scope. Here is what I mean:

#include <iostream>

class Foo {
   public:
    static const int A = 50;
    char arr[A];
    void showA();
};

void Foo::showA() {
    std::cout << "showA = " << A << "\n";
}

int main() {
    Foo f;
    f.showA();
}

当调试器在 showA() 中时,观察"窗口报告:

When the debugger is in showA() the "watch" window reports:

Error: Symbol "Foo::A" not found

我想强调的是该程序确实按预期运行,即输出是:

I'd like to emphasize that the program does behave as expected i.e. the output is:

showA = 50

程序返回0.

其他人能用 Visual Studio 2010 重现这个吗?这是调试器中的错误吗?

Can anyone else reproduce this with Visual Studio 2010? Is this a bug in the debugger?

推荐答案

Visual C++ 错误地提供了一个弱定义(此答案中提供了证据) 基于类内部的声明,尽管标准中有明确的语言:

Visual C++ erroneously provides a weak definition (evidence provided in this answer) based on the declaration inside the class, despite clear language in the Standard:

静态数据成员在其类定义中的声明不是定义,并且可能是除 cv 限定的 void 之外的不完整类型.静态数据成员的定义应出现在包含成员类定义的命名空间范围内.在命名空间范围的定义中,静态数据成员的名称应使用 :: 运算符由其类名限定.

The declaration of a static data member in its class definition is not a definition and may be of an incomplete type other than cv-qualified void. The definition for a static data member shall appear in a namespace scope enclosing the member’s class definition. In the definition at namespace scope, the name of the static data member shall be qualified by its class name using the :: operator.

根据标准中的另一条规则,如果成员不是odr-used,则不需要定义.

According to the another rule in the Standard, no definition is needed if the member isn't odr-used.

Visual C++ 是否错误地提供了显式定义或弱定义并没有区别.如果成员不是odr-used,链接器将看不到任何对它的引用并将其删除,让调试器不知道它是否曾经存在过.借助 Microsoft 链接器,您可以使用 /OPT:NOREF 禁止此优化.

Whether there's an explicit definition or weak definition erroneously provided by Visual C++ makes no difference however. If the member isn't odr-used, the linker will not see any references to it and will remove it, leaving the debugger confused about whether it ever existed. With the Microsoft linker, you can inhibit this optimization using /OPT:NOREF.

最终,这不是您想在生产代码中执行的操作,因为您的应用程序中会遗留标准库中的各种残留内容.但是为了在调试期间临时使用,这是一个合理的设置.

Ultimately that's not something you want to do in production code, though, since you'll have all kinds of vestigial stuff from the standard library left over in your application. But for temporary use during debugging that's a reasonable setting.

这篇关于为什么 Visual Studio 2010 调试器看不到静态常量类成员?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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