I would like to overload std :: swap in my template class. In the following code (simplified)
#ifndef Point2D_H #define Point2D_H template <class T> class Point2D { protected: T x; T y; public: Point2D () : x ( 0 ), y ( 0 ) {} Point2D( const T &x_, const T &y_ ) : x ( x_ ), y ( y_ ) {} .... public: void swap ( Point2D <T> &p ); }; template <class T> inline void swap ( Point2D <T> &p1, Point2D <T> &p2 ) { p1.swap ( p2 ); } namespace std { template <class T> inline void swap ( Point2D <T> &p1, Point2D <T> &p2 ) { p1.swap ( p2 ); } } template <class T> void Point2D <T>::swap ( Point2D <T> &p ) { using (std::swap); swap ( x, px ); swap ( y, py ); } #endif
there is a compiler error (only in VS 2010):
error C2668: 'std::swap' : ambiguous call to overloaded
I donβt know why, std :: swap should be overloaded ... Using g ++ code works fine. Without templates (i.e. Point2D is not a template class) this code also works.
Thank you for your help.
source share