syntax problem for partial template specialization for a template class.

Discussion in 'C++' started by toton, Dec 28, 2006.

  1. toton

    toton Guest

    Hi,
    I am doing some template specialization for a template class, where
    specialization is done on a member function. I am not able to get exact
    syntax for it.
    The code is give as below,
    enum DirectionType{
    dtX,dtY,dtXD,dtYD
    };
    template<typename T>
    class Point{
    T x_;
    T y_;
    T xd_;
    T yd_;
    T p_;
    T t_;
    public:
    Point(T x,T y,T p=0,T t=0): x_(x),y_(y),p_(p),t_(t){
    xd_= 5;
    yd_ =10;
    }
    template<DirectionType dt>
    T x()const;
    template<DirectionType dt>
    T y()const;
    };
    template<typename T>
    template<>
    T Point<T>::x<dtX>()const{
    return x_;
    }
    template<typename T>
    template<>
    T Point<T>::x<dtY>()const{
    return -y_;
    }
    template<typename T>
    template<>
    T Point<T>::x<dtXD>()const{
    return xd_;
    }
    template<typename T>
    template<>
    T Point<T>::x<dtYD>()const{
    return -yd_;
    }
    int main(int argc,char** argv) {
    Point<int> p(2,4);
    std::cout<<p.x<dtX>()<<std::endl;
    return 0;
    }

    Here , the Point class is parameterized over a type which point holds.
    and its member functions x() and y() are parameterized over a
    directions type, which to be implemented for 4 directions. I am not
    passing this enum in x() & y() member as there are only 4 directions
    and results are known, thus I think no if else/ switch is needed inside
    x() & y(), instead I can do it with template specialization.
    But there is problem with the syntax.

    Thanks
    abir
     
    toton, Dec 28, 2006
    #1
    1. Advertising

  2. toton

    Guest

    I think the standard forbids such specializations where the enclosing
    (class) template
    is not specialized.

    Tolga Ceylan
     
    , Dec 28, 2006
    #2
    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. Petre Iantu
    Replies:
    1
    Views:
    365
    Petre Iantu
    Aug 17, 2003
  2. Alfonso Morra
    Replies:
    1
    Views:
    312
    Victor Bazarov
    Aug 24, 2005
  3. shuisheng
    Replies:
    2
    Views:
    340
    shuisheng
    Sep 30, 2006
  4. vj
    Replies:
    1
    Views:
    502
  5. Hizo
    Replies:
    17
    Views:
    711
    itaj sherman
    Mar 7, 2011
Loading...

Share This Page