A coder's home for Marc "Foddex" Oude Kotte, who used to be located in Enschede, The Netherlands, but now in Stockholm, Sweden!
foddex.net

C++ static template members

Originally posted at Fri 07-03-2008 16:05:19, in the c++ category.
I'm always looking for new fun ways to abuse...^H^H^H^H^Huse C++. Today I stumbled on a fun difference between MSVC++ and gcc, the problem mainly being that MSVC++ has its own notion of what C++ is, and gcc simply following the appropriate standard(s).

Check out the following code.
template<typename _T>
class A {
	_T foo;
};

template<typename _T>
class B {
	public:
		struct subB {
			static A<float> myA;
			const char* name;
			subB( const char* _name ) : name( _name ) {}
		};
		static typename B<_T>::subB mySubB;
};

Suppose you use class B with type int. Then you need to define the two static members in the class. How to do this? My instinct said I needed to do this:
A<float> B<int>::subB::myA;
B<int>::subB B<int>::mySubB( "int" ); 

And, in fact, MSVC++ eats this eagerly and does exactly what you'd expect. However, using gcc to compile this code I got the following error on both lines: too few template-parameter-lists.

After blinking at the error for 5 minutes, I decided to google for it and found out that - not at all unexpected - you have to do this in a more generic, template-like way:

template<typename _T> A<float> B<_T>::subB::myA;
template<typename _T> typename B<_T>::subB B<_T>::mySubB( "generic" );

Isn't that glorious C++ code? :D You gotta love this kinda stuff. And ofcourse, you can specialize this for specific types. Suppose we want the static variables to be instantiated differently when we use class B with type float. Then we type this:
template<> A<float> B<float>::subB::myA;
template<> B<float>::subB B<float>::mySubB( "float" );

Template specialization, you gotta love it! :D And now for the test:

int main( int, char** ) {
	
	printf( "%s\n", B<int>::mySubB.name );
	printf( "%s\n", B<float>::mySubB.name );
	return 0;
}
Which outputs the following:
generic
float
Exactly as you'd expect.

If you reached here without skipping any text, coolio ;)

-- Foddex



0 comment(s)

Name:
URL: (optional!)
Write your comment:
Answer this question to prove you're human:
What's the white stuff on top of mountains called?