Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why Doesn't the Visual Studio 2010 Debugger See static const Class Members?

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

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

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

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

and the program returns 0.

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

like image 707
user2141130 Avatar asked May 03 '13 18:05

user2141130


2 Answers

You could add a definition for your static data member at global namespace scope:

const int Foo::A;

Adding the static data member definition, which is not necessary but allowed, seems to solve your problem.

I tested this on a debug build with VS2010, and the value of A is correctly displayed in the debug window when the definition is present (while an error message is reported when the definition is missing, consistently with what you mention).

like image 196
Andy Prowl Avatar answered Oct 03 '22 01:10

Andy Prowl


This isn't a bug. The compiler can (and almost always will) optimize out static constant basic types. Instead of allocating storage for A, the compiler is just inlining the value of A into the compiled instructions.

Because A isn't stored anywhere, it has no address, so the debugger cannot view it.

like image 23
Collin Dauphinee Avatar answered Oct 03 '22 00:10

Collin Dauphinee