Specialization

Discussion in 'C++' started by Jon Slaughter, Sep 15, 2005.

  1. 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
     
    Jon Slaughter, Sep 15, 2005
    #1
    1. Advertising

  2. Jon Slaughter

    persenaama Guest

    template <unsigned int I, unsigned int J, typename X>
    struct T< Node<I,J,X> > : public Node<I,J,X>::Class
    {
    };
     
    persenaama, Sep 15, 2005
    #2
    1. Advertising

  3. "persenaama" <> wrote in message
    news:...
    > 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
     
    Jon Slaughter, Sep 15, 2005
    #3
  4. "Jon Slaughter" <> wrote in message
    news:...
    >
    > "persenaama" <> wrote in message
    > news:...
    >> 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



    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
     
    Jon Slaughter, Sep 15, 2005
    #4
  5. Jon Slaughter

    persenaama Guest

    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..
     
    persenaama, Sep 16, 2005
    #5
  6. "persenaama" <> wrote in message
    news:...
    >
    > 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?


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




    Thanks.

    jon
     
    Jon Slaughter, Sep 16, 2005
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Vidur Dhanda
    Replies:
    0
    Views:
    384
    Vidur Dhanda
    Feb 19, 2005
  2. yource
    Replies:
    1
    Views:
    408
    Roedy Green
    Oct 27, 2005
  3. Johan Nilsson
    Replies:
    5
    Views:
    2,588
    Victor Bazarov
    Jun 25, 2003
  4. Chandra Shekhar Kumar

    template partial specialization

    Chandra Shekhar Kumar, Jun 24, 2003, in forum: C++
    Replies:
    17
    Views:
    3,069
    tom_usenet
    Jun 25, 2003
  5. Joseph Turian
    Replies:
    2
    Views:
    501
Loading...

Share This Page