Namespace specialization

K

Kufa

Hi,

I am probably asking for something which was discussed several times,
but all my researches were unsucessfull.

If you look at this code (simplified part of my project):

---------------
template< int dimension > struct Test
{
template< class A > struct assignement
{
template< int I > struct recurse
{
enum { Counter = I + 1 };
static inline void assign( void* v, const A& a )
{
recurse<Counter>::assign( v, a );
}
};

template<> struct recurse<dimension>
{
enum { Counter = dimension + 1 };
static inline void assign( void*, const A& )
{
}
};

static inline void assign( void* v, const A& a )
{
recurse<0>::assign( v, a );
}
};
};

---------

This code compiles fine with vs2005, but not with g++, which gives me
errors on my specialization template<> struct recurse<dimension>:

error: invalid explicit specialization before '>' token
error: explicit specialization in non-namespace scope `struct
Test<dimension>::assignement<A>'
error: enclosing class templates are not explicitly specialized
error: template parameters not used in partial specialization:
error: `A'

Even though i thought this was completely correct, i tried to put it
outside the class definition, but couldnt get it working.

So i am wondering if what i am doing is actually proper c++, or just
something vs allows? And if you would have a work around..

Thanks!
 
P

Patrick Kowalzick

I am probably asking for something which was discussed several times,
but all my researches were unsucessfull.

If you look at this code (simplified part of my project):

---------------
template< int dimension > struct Test
{
template< class A > struct assignement
{
template< int I > struct recurse
{
enum { Counter = I + 1 };
static inline void assign( void* v, const A& a )
{
recurse<Counter>::assign( v, a );
}
};

template<> struct recurse<dimension>
{
enum { Counter = dimension + 1 };
static inline void assign( void*, const A& )
{
}
};

static inline void assign( void* v, const A& a )
{
recurse<0>::assign( v, a );
}
};
};

---------

This code compiles fine with vs2005, but not with g++, which gives me
errors on my specialization template<> struct recurse<dimension>:

error: invalid explicit specialization before '>' token
error: explicit specialization in non-namespace scope `struct
Test<dimension>::assignement<A>'
error: enclosing class templates are not explicitly specialized
error: template parameters not used in partial specialization:
error: `A'

Even though i thought this was completely correct, i tried to put it
outside the class definition, but couldnt get it working.

So i am wondering if what i am doing is actually proper c++, or just
something vs allows? And if you would have a work around..

MSVC is wrong in this case and g++ correct. Explicit template specialization
is forbidden for nested classes.

As partial template specialization is not forbidden, you get around quite
comfortable. I did not test it, if it is not working, I have to dig in my
code:

template< int dimension > struct Test
{
template< class A > struct assignement
{
template< int I, typename T = void > struct recurse
{
enum { Counter = I + 1 };
static inline void assign( void* v, const A& a )
{
recurse<Counter>::assign( v, a );
}
};

template< typename T > struct recurse<dimension, T>
{
enum { Counter = dimension + 1 };
static inline void assign( void*, const A& )
{
}
};

static inline void assign( void* v, const A& a )
{
recurse<0>::assign( v, a );
}
};
};
 
K

Kufa

Thanks Patrick,
I didn't know explicit template specialization is forbidden for nested
classes. Your change works indeed fine, except when i put this part
back into the main code, it looks infinitely (my terminator is not
used).

/kUfa
 
P

Patrick Kowalzick

Thanks Patrick,
I didn't know explicit template specialization is forbidden for nested
classes. Your change works indeed fine, except when i put this part
back into the main code, it looks infinitely (my terminator is not
used).

Hello Kufa,

I can confirm the infinitive loop for VS2003 and gcc. It looks like a bug to
me.

If you can iterate backwards, you can partial specialize to 0, what works
fine.

Patrick
 
B

Bo Persson

MSVC is wrong in this case and g++ correct. Explicit template
specialization is forbidden for nested classes.

It's an MS extension, from way back. If you add the Disable Language
Extensions option to the compile, the code will be rejected.


Bo Persson
 

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,780
Messages
2,569,614
Members
45,292
Latest member
EttaCasill

Latest Threads

Top