Help | Site Map
Connecting Tech Pros Worldwide
 
 
LinkBack Thread Tools
  #1  
Old July 19th, 2005, 08:40 PM
Nikolai Borissov
Guest
 
Posts: n/a
Default Specialization of a template within template

Is it possible to specialize a templated class defined within another
templeted class, like below:

template<typename U>
struct A
{ template<typename T>
struct B;
};

// Attempt to specialize internal class B for type int
template<typename U>
struct A<U>::B<int> {};

typedef A<long>::B<int> MyType; // this causes a failure

Online Comeau C++ fails without a message.
What is the right syntax, if any, for such specializations ?

Thanks,

Nikolai Borissov


  #2  
Old July 19th, 2005, 08:40 PM
David B. Held
Guest
 
Posts: n/a
Default Re: Specialization of a template within template

"Nikolai Borissov" <n.borissov@sympatico.ca> wrote in message
news:iVGmb.10683$7t3.492363@news20.bellglobal.com. ..[color=blue]
> Is it possible to specialize a templated class defined within
> another templeted class, like below:
> [...][/color]

I've been told that it isn't legal C++, but looking at 14.7 makes
me think otherwise. I don't have my copy of C++T handy, so
I can't look there. You might want to ask in c.l.c++.m, as some
more knowledgeable readers tend to visit there.

Dave



---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.521 / Virus Database: 319 - Release Date: 9/23/2003


  #3  
Old July 19th, 2005, 08:40 PM
Rob Williscroft
Guest
 
Posts: n/a
Default Re: Specialization of a template within template

Nikolai Borissov wrote in news:iVGmb.10683$7t3.492363
@news20.bellglobal.com:
[color=blue]
> Is it possible to specialize a templated class defined within another
> templeted class, like below:
>
> template<typename U>
> struct A
> { template<typename T>
> struct B;
> };
>
> // Attempt to specialize internal class B for type int[/color]

I changed this to:
template <typename U> template <>
struct A<U>::B<int> {};

gcc 3.2:
test.cpp:14: enclosing class templates are not explicitly specialized
test.cpp:15: template parameters not used in partial specialization:
test.cpp:15: `U'

msvc 7.1: test.cpp:14: error C3212: 'A<U>::B<int>' : an explicit
specialization of a template member must be a member of an explicit
specialization
test.cpp:15: see declaration of 'A<U>::B<int>'

online EDG: a template declaration containing a template parameter list
may not be followed by an explicit specialization declaration
template <typename U> template <>
[color=blue]
> template<typename U>
> struct A<U>::B<int> {};
>
> typedef A<long>::B<int> MyType; // this causes a failure
>
> Online Comeau C++ fails without a message.
> What is the right syntax, if any, for such specializations ?
>[/color]

I don't think there is any.

I'd suggest switching the problem around:

template <typename T>
struct B_inner
{
template<typename U>
struct A_outer {};
};


/* Specialize this as meny times as we want
*/
template <>
struct B_inner< int >
{
template <typename U>
struct A_outer
{
};
};

template<typename U>
struct A
{
template <typename T>
struct B : public B_inner< T >::template A_outer< U >
{
};
};

typedef A<long>::B<int> MyType; // this causes a failure

HTH

Rob.
--
http://www.victim-prime.dsl.pipex.com/
 

Bookmarks

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 Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over network members.
Post your question now . . .
It's fast and it's free

Popular Articles