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?
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).
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With