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

H

huili80

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;
};
*/
};
 
H

huili80

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.
 
G

Greg Herlihy

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<int> explicit specialization - without explicitly specializing A
as well.

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

Greg
 
H

huili80

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<int> explicit specialization - without explicitly specializing A
as well.

In other words, the problem with the above program is that B<int> is
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;
};
 
H

huili80

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!!!
 
H

huili80

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<int> explicit specialization - without explicitly specializing A
as well.

In other words, the problem with the above program is that B<int> is
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<int> b;
A():b(*this){}
};


int main()
{
A<int> a;
A<int>::B<double> b1(a);

if ( b1.v != 1 )
throw;

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

return 0;
};
 

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

Forum statistics

Threads
473,731
Messages
2,569,432
Members
44,834
Latest member
BuyCannaLabsCBD

Latest Threads

Top