Nested templates explicit specialization

A

Andriy Shnyr

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
 
D

Dan W.

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

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

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

Cheers!
 
V

Victor Bazarov

Andriy Shnyr said:
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
 
L

Larry Evans

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top