Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Nested templates explicit specialization

Reply
Thread Tools

Nested templates explicit specialization

 
 
Andriy Shnyr
Guest
Posts: n/a
 
      12-03-2003
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:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]

 
Reply With Quote
 
 
 
 
Dan W.
Guest
Posts: n/a
 
      12-03-2003
On Wed, 3 Dec 2003 17:35:21 +0000 (UTC), (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:std-c++@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!




 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      12-03-2003
"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?


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


 
Reply With Quote
 
Larry Evans
Guest
Posts: n/a
 
      12-04-2003
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:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
nested templates and partial specialization ld C++ 8 11-12-2011 03:18 PM
Explicit instantiation of STL vector demands explicit instantiation of all the templates it using internally. krunalbauskar@gmail.com C++ 1 12-25-2006 03:51 PM
Why does Explicit specialization of function templates cause generation of code? Vyacheslav Lanovets C++ 6 08-24-2005 06:43 AM
What's the difference betwwen explicit instantiaion and explicit specialization? Andy C++ 5 01-30-2005 11:46 PM
Explicit and partial template specialization nested in classes. Patrick Kowalzick C++ 0 10-29-2004 02:45 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57