Re: Problem with static data members in templated class
* Andy, on 04.10.2010 14:11:
I have some code (below) that is giving me problems to compile.
Yes, I suspect your code is invalid even when corrected for syntax etc., but
although Comeau Online agrees with that sentiment I'm not 100% sure -- using
one static member of a class template to initialize another just could be
allowed, or not explicitly disallowed, or it could be like static initialization
order fiasco (some hours of study might resolve the question).
Anyway, fixing up the code it caused an internal compiler error in MinGW g++
4.4.1. I've reported that at <url:
https://sourceforge.net/tracker/?func=detail&aid=3081134&group_id 0665&atid=974439>.
So it seems you were battling three hostile forces at the same time: a design
problem, a corner case or murky territory of the Holy Standard, and a g++
compiler bug. :-)
You can probably use the following workaround, but I suggest looking hard at
your design, thinking about what you're trying to achieve, how responsibilities
should be allocated, and what knowledge is needed where to do things:
<code>
#include <stdio.h>
template< char* p >
struct C1
{
char operator()() const { return *p; }
};
struct C2
{
static char m;
static C1<&m> op;
};
char C2::m = 'a';
C1<&C2::m> C2::op;
template< class T >
struct C3m
{
static char m;
};
template< class T > char C3m<T>::m = '0';
template<> char C3m<int>::m = 'b';
template< class T >
struct C3
{
static C1<&C3m<T>::m> op1;
static C1<&C2::m> op2;
};
template< class T > C1<&C3m<T>::m> C3<T>::op1;
template<> C1<&C3m<int>::m> C3<int>::op1;
template< class T > C1<&C2::m> C3<T>::op2;
template<> C1<&C2::m> C3<int>::op2;
int main()
{
printf("%c\n", C2::op());
printf("%c\n", C3<int>::op1());
printf("%c\n", C3<int>::op2());
}
</code>
Cheers & hth.,
- Alf
--
blog at <url: http://alfps.wordpress.com>