T
toton
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
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