为什么Visual Studio 2010调试程序不查看static const类成员? [英] Why Doesn't the Visual Studio 2010 Debugger See static const Class Members?

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

问题描述

这个问题与后续的问题密切相关

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 / p>

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调试程序不查看static const类成员?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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