Why Can't VS 2010 Debugger Show a 'static const' class data member?

C++ Gurus,

I'm in the process of learning C++, and make every effort to use the Visual Studio 2010 debugger to aid in that process. Right now, I'm exploring static class data members.

I've run into an issue where the debugger is unable to find / display a 'static const' class data member. And am wondering if somebody here can explain why I can't add that entity to the debugger's "watch list". It's interesting, in that I can get the debugger to dispaly 'static' (but not const) class data members.

Here's the simple code I'm using ...

DemoStaticClassMembers.h:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
//
// Toy class for learning about static class member initilization
// and accessing rules.
//

using namespace std;

class ToyClass {

public:

	ToyClass(int a, int b) {
		cout << "Running Constructor\n";
		Object_Data_Member1 = a;
		Object_Data_Member2 = b;
	}

	static const int  Static_Class_Member1 = 1234;    // initialize "in place"
	static int        Static_Class_Member2;           // don't initialize

private:
	
	int Object_Data_Member1;
	int Object_Data_Member2;

};


... and DemoStaticClassMembers.cpp:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
//
// Simple program to demonstrate the usage of Static class data members, and
// to figure out why Visual Studio debugger can't display static const class data members.
//

#include <iostream>
#include "DemoStaticClassMembers.h"

using namespace std;

int ToyClass::Static_Class_Member2 = 94;   // have to define value in file scope (not within a function)

int main(int argc, char* argv[])  {

	cout << "Pre-Object Creation: \n";
	cout << "  Initialized static const 'Static_Class_Member1' = " << ToyClass::Static_Class_Member1 << "\n";
	cout << "  Uninitialized static 'Static_Class_Member2' = " << ToyClass::Static_Class_Member2 << "\n";
	
	ToyClass Object1(1,2);   
	ToyClass Object2(3,4);

	// ToyClass::Static_Class_Member1 = 16;  // will compiler let me do this?  No.  Cannot modify a const.
	// ToyClass::Static_Class_Member2 = 4;   // will compiler let me do this?  Yes.  It compiles OK - but linker
	                                         // stage fails because this member hasn't been defined outside of a function

	cout << "Post-Object Creation: \n";
	cout << "  Initialized static const 'Static_Class_Member1' = " << ToyClass::Static_Class_Member1 << "\n";
	cout << "  Uninitialized static 'Static_Class_Member2' = " << ToyClass::Static_Class_Member2 << "\n";

	
}


The Static_Class_Member1 class data member really exists, and it got initialized as expected (witness the program's output) ...


Pre-Object Creation:
  Initialized static const 'Static_Class_Member1' = 1234
  Uninitialized static 'Static_Class_Member2' = 94
Running Constructor
Running Constructor
Post-Object Creation:
  Initialized static const 'Static_Class_Member1' = 1234
  Uninitialized static 'Static_Class_Member2' = 94



Yet, if I try to add ToyClass::Static_Class_Member1 to the debugger's "watch list", I get an error message saying "CXX0070: Error: static member not present".

Is there some other notation I need to use to get the debugger to show me the contents of that 'static const' class data member?

Is this a "bug" in the debugger?

Thanks to all (in advance) who can enlighten me.

Last edited on
Static consts can be completely optimized out by the compiler, especially if they are compiler-intrinsic POD types. The debugger isn't able to watch it because there is no memory assigned to the const.
D'oh! Thanks for that explanation. Makes sense to me now.

That (probably) also explains why I can't do an "in place" initialization of some non-standard type (e.g. a user defined structure) within the class declaration/definition itself.
Yes, in a way. If the compiler _had_ to allocate memory for it, then every translation unit (read: .cpp file) that included that header file would cause the compiler to want to reserve memory for it, so you'd end up with N copies of the variable in the initialized data segment in the executable, which is a problem. But if no memory is needed for the constant, then the problem is avoided. Hence why it is the way it is.
Thanks again, jsmith. It's good that there are smart (and experienced) people - like you - who are willing to use their own time to educate us newcomers. Much appreciated.
Topic archived. No new replies allowed.