469,575 Members | 1,613 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

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

how to partially specialize a template class nested inside anothertemplate class?

For example, like in the following, the part commented out was
intended as partial spectialzation, but it would even compile. Is it
even legal to partially specialize a nested template class inside
another template class?

template < typename T >
struct A
{
template < typename U >
struct B
{
static const int v = 1;
};

/* template < // this doesn't compile
struct B < int >
{
static const int v = 3;
};
*/
};
Jun 27 '08 #1
5 3404
On Jun 22, 12:15*am, huil...@gmail.com wrote:
For example, like in the following, the part commented out was
intended as partial spectialzation, but it would even compile. *Is it
even legal to partially specialize a nested template class inside
another template class?

template < typename T >
struct A
{
* * * * template < typename U >
* * * * struct B
* * * * {
* * * * * * * * static const int v = 1;
* * * * };

/* * * *template < // this doesn't compile
* * * * struct B < int >
* * * * {
* * * * * * * * static const int v = 3;
* * * * };
*/

};

Sorry, when I said the could "would even compile", I meant "wouldn't
even compile". Sorry about this typo.
Jun 27 '08 #2
On Jun 21, 9:15*pm, huil...@gmail.com wrote:
For example, like in the following, the part commented out was
intended as partial spectialzation, but it would even compile. *Is it
even legal to partially specialize a nested template class inside
another template class?

template < typename T >
struct A
{
* * * * template < typename U >
* * * * struct B
* * * * {
* * * * * * * * static const int v = 1;
* * * * };

/* * * *template < // this doesn't compile
* * * * struct B < int >
* * * * {
* * * * * * * * static const int v = 3;
* * * * };
*/

};
Yes, it is possible to partially specialize a nested class template in
C++ (without having to specialize the enclosing class template). It is
not legal however to explicitly specialize a nested class template (if
the enclosing class template is not explicitly specialized as well).
In fact, the example program demonstrates this last point by defining
a B<intexplicit specialization - without explicitly specializing A
as well.

In other words, the problem with the above program is that B<intis
not a partial specialization - but a complete specialization of B.

Greg

Jun 27 '08 #3
On Jun 22, 6:33*am, Greg Herlihy <gre...@mac.comwrote:
On Jun 21, 9:15*pm, huil...@gmail.com wrote:
For example, like in the following, the part commented out was
intended as partial spectialzation, but it would even compile. *Is it
even legal to partially specialize a nested template class inside
another template class?
template < typename T >
struct A
{
* * * * template < typename U >
* * * * struct B
* * * * {
* * * * * * * * static const int v = 1;
* * * * };
/* * * *template < // this doesn't compile
* * * * struct B < int >
* * * * {
* * * * * * * * static const int v = 3;
* * * * };
*/
};

Yes, it is possible to partially specialize a nested class template in
C++ (without having to specialize the enclosing class template). It is
not legal however to explicitly specialize a nested class template (if
the enclosing class template is not explicitly specialized as well).
In fact, the example program demonstrates this last point by defining
a B<intexplicit specialization - without explicitly specializing A
as well.

In other words, the problem with the above program is that B<intis
not a partial specialization - but a complete specialization of B.

Greg
Thanks! But how do I understand the example in the following. Here
B<C<V looks like an explicit specialization just like B<int>, since
C is just another template independent of A or A::B. But now it
compiles on apple-g++-4.0, and gives the correct (expected) result (no
exception was thrown).

-------------------------------------------------------------

template < typename T >
struct C {};

template < typename T >
struct A
{
template < typename U >
struct B
{
static const int v = 1;
};

template < typename V >
struct B< C<V
{
static const int v = 3;
};
};

int main()
{
if ( A<int>::B<double>::v != 1 )
throw;

if ( A<int>::B< C<double::v != 3 )
throw;

return 0;
};

Jun 27 '08 #4
On Jun 22, 9:29*am, huil...@gmail.com wrote:
On Jun 22, 6:33*am, Greg Herlihy <gre...@mac.comwrote:
On Jun 21, 9:15*pm, huil...@gmail.com wrote:
For example, like in the following, the part commented out was
intended as partial spectialzation, but it would even compile. *Is it
even legal to partially specialize a nested template class inside
another template class?
template < typename T >
struct A
{
* * * * template < typename U >
* * * * struct B
* * * * {
* * * * * * * * static const int v = 1;
* * * * };
/* * * *template < // this doesn't compile
* * * * struct B < int >
* * * * {
* * * * * * * * static const int v = 3;
* * * * };
*/
};
Yes, it is possible to partially specialize a nested class template in
C++ (without having to specialize the enclosing class template). It is
not legal however to explicitly specialize a nested class template (if
the enclosing class template is not explicitly specialized as well).
In fact, the example program demonstrates this last point by defining
a B<intexplicit specialization - without explicitly specializing A
as well.
In other words, the problem with the above program is that B<intis
not a partial specialization - but a complete specialization of B.
Greg

Thanks! But how do I understand the example in the following. Here
B<C<V looks like an explicit specialization just like B<int>, since
C is just another template independent of A or A::B. *But now it
compiles on apple-g++-4.0, and gives the correct (expected) result (no
exception was thrown).

-------------------------------------------------------------

template < typename T >
struct C {};

template < typename T >
struct A
{
* * * * template < typename U >
* * * * struct B
* * * * {
* * * * * * * * static const int v = 1;
* * * * };

* * * * template < typename V >
* * * * struct B< C<V
* * * * {
* * * * * * * * static const int v = 3;
* * * * };

};

int main()
{
* * * * if ( A<int>::B<double>::v != 1 )
* * * * * * * * throw;

* * * * if ( A<int>::B< C<double::v != 3 )
* * * * * * * * throw;

* * * * return 0;

};

Never mind.... B<C<V is not an explicit specialization. What was I
thinking!!!
Jun 27 '08 #5
On Jun 22, 6:33*am, Greg Herlihy <gre...@mac.comwrote:
On Jun 21, 9:15*pm, huil...@gmail.com wrote:
For example, like in the following, the part commented out was
intended as partial spectialzation, but it would even compile. *Is it
even legal to partially specialize a nested template class inside
another template class?
template < typename T >
struct A
{
* * * * template < typename U >
* * * * struct B
* * * * {
* * * * * * * * static const int v = 1;
* * * * };
/* * * *template < // this doesn't compile
* * * * struct B < int >
* * * * {
* * * * * * * * static const int v = 3;
* * * * };
*/
};

Yes, it is possible to partially specialize a nested class template in
C++ (without having to specialize the enclosing class template). It is
not legal however to explicitly specialize a nested class template (if
the enclosing class template is not explicitly specialized as well).
In fact, the example program demonstrates this last point by defining
a B<intexplicit specialization - without explicitly specializing A
as well.

In other words, the problem with the above program is that B<intis
not a partial specialization - but a complete specialization of B.

Greg
Hi, I found a way (among other possibilities) to workaround impossible
explicit specialization of nested template classes.
It works!
---------------------------------------------------------------------
template < typename struct A; // forward declaration of A
template < typename T, typename t >
struct C
{
typedef A<t> AT;
static const int v = 1;
};

template < typename t >
struct C<int,t>
{
typedef A<t> AT;
static const int v = 3;
};
template < typename T >
struct A
{
template < typename U >
struct B : C<U,T>
{
typename C<U,T>::AT const& _a;
B(typename C<U,T>::AT const& a):_a(a){}
};

B<intb;
A():b(*this){}
};
int main()
{
A<inta;
A<int>::B<doubleb1(a);

if ( b1.v != 1 )
throw;

if ( a.b.v != 3 )
throw;

return 0;
};
Jun 27 '08 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Dominik Fritz | last post: by
3 posts views Thread by PengYu.UT | last post: by
7 posts views Thread by mathieu | last post: by
4 posts views Thread by guiromero | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.