 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Mikael Andersson Guest
|
Posted: Sat Aug 12, 2006 1:28 am Post subject: Unresolved externals on static members in nested templates |
|
|
I'm using nested templates in an access-time optimisation storage class
and I'm trying to use static members to store commonly data which values
are calcualable at compile time. However, static members in nested
templates consitently result in unresolved external link errors when
compiling with Visual Studio 2005's compiler (I have not tried any other
compiler). Boiling down the problem to the bones leaves me with this
structure:
template<typename T>
struct Outer
{
static int outer_member;
template<int TT> struct Inner;
template<> struct Inner<1>
{
static int inner_member;
};
};
template<typename T> int Outer<T>::outer_member = 0;
template<typename T> template<int TT> int
Outer<T>::Inner<1>::inner_member = 0;
int main()
{
Outer<void> foo;
Outer<void>::Inner<1> bar;
// ok
foo.outer_member = 1;
//unresolved external
bar.inner_member = 1;
//unresolved external
Outer<void>::Inner<1>::inner_member = 1;
}
Not using a templated Inner class, or using templated but not
specialised Inner classes, makes the program compile. Thus, the question
is: what am I doing wrong? Or is it a compiler issue?
Regards,
Michael Andersson
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ] |
|
| Back to top |
|
 |
Greg Herlihy Guest
|
Posted: Sat Aug 12, 2006 4:29 am Post subject: Re: Unresolved externals on static members in nested templat |
|
|
Mikael Andersson wrote:
| Quote: | I'm using nested templates in an access-time optimisation storage class
and I'm trying to use static members to store commonly data which values
are calcualable at compile time. However, static members in nested
templates consitently result in unresolved external link errors when
compiling with Visual Studio 2005's compiler (I have not tried any other
compiler). Boiling down the problem to the bones leaves me with this
structure:
template<typename T
struct Outer
{
static int outer_member;
template<int TT> struct Inner;
template<> struct Inner<1
{
static int inner_member;
};
};
|
It's not legal to specialize an inner class template without
specializing each enclosing class template. And although VC may allow
it, it's not standard C++.
| Quote: | template<typename T> int Outer<T>::outer_member = 0;
|
This definition is OK.
| Quote: | template<typename T> template<int TT> int
Outer<T>::Inner<1>::inner_member = 0;
|
This definition is not. As noted above, the Inner class template cannot
be specialized without the Outer class template being specialized as
well.
One way of solving this problem would be to "reverse" the Inner and
Outer class templates. Moreover, since the concepts of "inner" and
"outer" when applied to templates more aptly refer to levels of
template instantiation rather layers of nested classes - it would make
sense to use an instantiation of one class template to instantiatate
the other.
So implementing these suggestions would produce a program like the
following:
// Outer class template
template<typename T>
struct Outer
{
static int outer_member;
};
// Outer::outer_member definition
template <class T> int Outer<T>::outer_member = 0;
// Inner class template
template <class T, int N> struct Inner;
// Inner class template partial specialization
template <class T>
struct Inner<Outer<T>, 1>
{
static int inner_member;
};
// Inner::inner_member definition (partial specialization)
template <class T>
int Inner<Outer<T>, 1>::inner_member = 0;
int main()
{
Outer<void> foo;
foo.outer_member = 1;
Inner<Outer<void>, 1>::inner_member = 1;
}
Greg
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ] |
|
| Back to top |
|
 |
Mikademus Guest
|
Posted: Sun Aug 13, 2006 4:29 pm Post subject: Re: Unresolved externals on static members in nested templat |
|
|
| Quote: | One way of solving this problem would be to "reverse" the Inner and
Outer class templates. Moreover, since the concepts of "inner" and
"outer" when applied to templates more aptly refer to levels of
template instantiation rather layers of nested classes - it would
make
sense to use an instantiation of one class template to instantiatate
the other.
|
Thank you. I followed your advice and rewrote the class hierarchy along
your suggestion. Nesting the classes was conventíent for reducing
namespece
cluttering, but that is a small price to pay for more efficiently
working structure.
| Quote: | It's not legal to specialize an inner class template without
specializing each enclosing class template. And although VC may allow
it, it's not standard C++.
|
Is that something that might be revised in later specifications of the
Standard? It seems of a similar nature to alias typedefs on templates,
which apparently is something C++0x will include.
Regards,
Michael Andersson
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ] |
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|