Specialization

J

Jon Slaughter

I'm having a big problem(its probably simple but I just can't seem to figure
it out).

I have a Node template:

template <unsigned int I, unsigned int J, typename T>
struct Node
{
enum {i = I};
enum {j = J};
typedef T Class;
};


Now I have a template class I'm trying to pass a Node to

template <class A>
struct T
{
.....
};

and I would do something like T<Node<3,2,someclass>> B;

Now what I want to do is specialize T so that it if A is of Node type then
it will inherit Node::Class i.e.

template <class A>
struct T : public A::Class
{
}


which works fine... but this isn't specializing T. I've tried to do

template <class A>
struct T
{
};

template <class A>
struct T<Node> : public A::Class
{

};

but that doesn't work... I've tried to use template template parameters and
a whole lot of other crap but nothing works ;/

I need to specialize T because in reality I will be having a variable number
of Nodes... hmm.. I just can't figure out why the above won't work(ofcourse
its cause I'm weak at templates but thats besides the point).

To me, the way I see it is that when I do

T<Node<1,2,someclass>> the compiler will find the specialization of T that
is being passed a note type(or does it also try to match the exact type such
as 1,2,class?) and then everything should work...

Just not sure what to do since I get some errors that don't make any sense
to me like:

'Node' : unspecialized class template can't be used as a template argument
for template parameter 'A', expected a real type.


Any ideas how to do this?

Thanks, Jon
 
P

persenaama

template <unsigned int I, unsigned int J, typename X>
struct T< Node<I,J,X> > : public Node<I,J,X>::Class
{
};
 
J

Jon Slaughter

persenaama said:
template <unsigned int I, unsigned int J, typename X>
struct T< Node<I,J,X> > : public Node<I,J,X>::Class
{
};

Thanks, I tried something similar but I was not getting the public part
right. I think I see now. (I was trying to do it directly from X such as
public X::Class thinking that it would know but I guess thats not right).

Jon
 
J

Jon Slaughter

Jon Slaughter said:
Thanks, I tried something similar but I was not getting the public part
right. I think I see now. (I was trying to do it directly from X such as
public X::Class thinking that it would know but I guess thats not right).

Jon


crap ;/ I can't seem to be able to do what I need to do.


template <unsigned int I, unsigned int J, typename T>
struct Node
{
enum {i = I};
enum {j = J};
typedef T Class;
};


template <int i, typename T>
struct RT { };


template <int I, int J, typename T>
struct RT< Node<I,J,T>::i, Node<I,J,T> > : public Node<I,J,T>::Class
{
static const int t = Node<I,J,T>::i;
};


I have no problem access i when inside the struct above but I can't use it
to pass it to the RT specifier...

I figured that Node<I,J,T>::i would return an literal int and just stick it
in there as if I did

template <int I, int J, typename T>
struct RT<3, Node<I,J,T> > : public Node<I,J,T>::Class
{
static const int t = Node<I,J,T>::i;
};

(which works)

What am I missing here? (The ::Class part works fine but the ::i
doesn't... ) I assume I need to somehow "Cast" Node<I,J,T>::i to a simple
type but I have no idea if this is true or not and how to do it((int)
doesn't work)


error C2755: 'RT<Node<I,J,T>::i,Node<I,J,T>>' : non-type parameter of a
partial specialization must be a simple identifier


not sure what it means by simple identifier but I assume it doesn't realize
that Node<I,J,T>::i is suppose to be a literal int?

Is my approach wrong to do what I have did by using node to "encapsulate"
those types? (I tried to model it off the Int2Type and Type2Type in modern
C++ but I can't seem to extract the right stuff that works from his examples
;/)

Thanks
Jon
 
P

persenaama

template <int I, int J, typename T>
struct RT< I, Node<I,J,T> > : public Node<I,J,T>::Class
{
static const int t = Node<I,J,T>::i;

};
not sure what it means by simple identifier but I assume it
doesn't realize that Node<I,J,T>::i is suppose to be a literal int?

It does, after the types have been matched. At that point I suppose the
compiler is still trying to match the types to determine if that
specialization should be invoked or not only to find out that the
matching should be DONE ALREADY, just a hunch could be easily wrong.

The smart ones will flock here after a mistake so you're all set for
the correct answer. Now we just have to wait..
 
J

Jon Slaughter

persenaama said:
template <int I, int J, typename T>
struct RT< I, Node<I,J,T> > : public Node<I,J,T>::Class
{
static const int t = Node<I,J,T>::i;

};

duh ;) lol. I was up all last night so I have an excuse ;) I think I was
thinking the first I was part of the I in the template definition or
something.

I just wish I could do something like

template <Node T>
struct ...

And it treated T as a node type ;/ I guess it causses some issues or
something though and that why one has to use class name and specialization?

It does, after the types have been matched. At that point I suppose the
compiler is still trying to match the types to determine if that
specialization should be invoked or not only to find out that the
matching should be DONE ALREADY, just a hunch could be easily wrong.

The smart ones will flock here after a mistake so you're all set for
the correct answer. Now we just have to wait..



Thanks.

jon
 

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

No members online now.

Forum statistics

Threads
473,981
Messages
2,570,188
Members
46,731
Latest member
MarcyGipso

Latest Threads

Top