469,331 Members | 1,479 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,331 developers. It's quick & easy.

Nested templates explicit specialization

Let us consider the following nested templates case:

template<typename T>
class Outer{

public:
template<typename U>
class Inner{

public:
void f();
};

};

Then the following specialization
template<> template<typename U> void Outer<int>::Inner<U>::f(){};
results in compile-time error (I've tried to compile it with gcc 3.2.2)

main.cpp:XXX: invalid use of undefined type `class Outer<int>::Inner<U>'
main.cpp:XXX: declaration of `class Outer<int>::Inner<U>'
main.cpp:XXX: template definition of non-template `void
Outer<int>::Inner<U>::f()'

Can anyone help me to figure out the problem?

Thanks in advnace,
Andriy Shnyr

---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:st*****@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]

Jul 22 '05 #1
3 1881
On Wed, 3 Dec 2003 17:35:21 +0000 (UTC), sh***@mailcity.com (Andriy
Shnyr) wrote:
Let us consider the following nested templates case:

template<typename T>
class Outer{

public:
template<typename U>
class Inner{

public:
void f();
};

};

Then the following specialization
template<> template<typename U> void Outer<int>::Inner<U>::f(){};
results in compile-time error (I've tried to compile it with gcc 3.2.2)

main.cpp:XXX: invalid use of undefined type `class Outer<int>::Inner<U>'
main.cpp:XXX: declaration of `class Outer<int>::Inner<U>'
main.cpp:XXX: template definition of non-template `void
Outer<int>::Inner<U>::f()'

Can anyone help me to figure out the problem?

Thanks in advnace,
Andriy Shnyr

---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:st*****@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]


template < typename O >
struct outer
{
template < typename I >
struct inner
{
void f();
};
};

template <>
struct outer<int>
{
template < typename I >
struct Inner
{
void f();
};
};

Cheers!


Jul 22 '05 #2
"Andriy Shnyr" <sh***@mailcity.com> wrote...
Let us consider the following nested templates case:

template<typename T>
class Outer{

public:
template<typename U>
class Inner{

public:
void f();
};

};

Then the following specialization
template<> template<typename U> void Outer<int>::Inner<U>::f(){};
results in compile-time error (I've tried to compile it with gcc 3.2.2)

main.cpp:XXX: invalid use of undefined type `class Outer<int>::Inner<U>'
main.cpp:XXX: declaration of `class Outer<int>::Inner<U>'
main.cpp:XXX: template definition of non-template `void
Outer<int>::Inner<U>::f()'

Can anyone help me to figure out the problem?


If you need to specialise the outer class but keep the nested template
a template, you need to first declare the existence of 'Outer<int>::Inner'
template manually:

template<> class Outer<int> {
template<class U> class Inner {
public:
void f();
};
};

Otherwise you're declaring a member function of a class that doesn't
exist.

That's my take on it, anyway.

Victor
Jul 22 '05 #3
On 12/03/2003 11:35 AM, Andriy Shnyr wrote:
[snip]
main.cpp:XXX: invalid use of undefined type `class Outer<int>::Inner<U>'
main.cpp:XXX: declaration of `class Outer<int>::Inner<U>'
main.cpp:XXX: template definition of non-template `void
Outer<int>::Inner<U>::f()'

Can anyone help me to figure out the problem?

IIRC, adding ::template Inner<U> solved the compile problem for me;
however, I found a problem with the produced code not selecting the
right specialization, as reported here:

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=13088

---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:st*****@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]

Jul 22 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

15 posts views Thread by Robin Eidissen | last post: by
1 post views Thread by Arne Petersen | last post: by
reply views Thread by Patrick Kowalzick | last post: by
1 post views Thread by Thomas Barnet-Lamb | last post: by
3 posts views Thread by Steven T. Hatton | last post: by
9 posts views Thread by stephen.diverdi | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by suresh191 | last post: by
reply views Thread by Purva khokhar | last post: by
1 post views Thread by haryvincent176 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.