By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,877 Members | 1,115 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,877 IT Pros & Developers. It's quick & easy.

Specialization of a template within template

P: n/a
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
Jul 19 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
"Nikolai Borissov" <n.********@sympatico.ca> wrote in message
news:iV********************@news20.bellglobal.com. ..
Is it possible to specialize a templated class defined within
another templeted class, like below:
[...]


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
Jul 19 '05 #2

P: n/a
Nikolai Borissov wrote in news:iVGmb.10683$7t3.492363
@news20.bellglobal.com:
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
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 <>
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 ?


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/
Jul 19 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.