C++,重载 std::swap,编译器错误,VS 2010

发布于 2024-10-09 17:26:32 字数 1071 浏览 0 评论 0原文

我想在我的模板类中重载 std::swap 。在下面的代码(简化)中,

#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, p.x );
    swap ( y, p.y );
}

#endif

有一个编译器错误(仅在 VS 2010 中):

error C2668: 'std::swap' : ambiguous call to overloaded 

我不知道为什么,std::swap 应该被过载...使用 g ++ 代码工作得很好。如果没有模板(即 Point2D 不是模板类),此代码也可以工作。

感谢您的帮助。

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, p.x );
    swap ( y, p.y );
}

#endif

there is a compiler error (only in VS 2010):

error C2668: 'std::swap' : ambiguous call to overloaded 

I do not know why, std::swap should be overoaded... Using g ++ code works perfectly. Without templates (i.e. Point2D is not a template class) this code also works..

Thanks for your help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

初见你 2024-10-16 17:26:32

请参阅如何重载 std::swap() 。基本上,您可以对 std::swap 进行专门化,但不能进行重载。

因此,可以为特定的 Point<> 类型(例如 Point)创建特定版本,但不能为任何 Point创建特定版本;

Please see How to overload std::swap() . Basically, you are allowed to make a specialization of std::swap, but not an overload.

So, it is ok to create a specific version for a specific Point<> type (say Point<float>), but not for any Point<T>.

过期以后 2024-10-16 17:26:32

我知道您没有问过这个问题,但由于您使用的是 VC10,因此提供了一个移动构造函数和一个移动赋值运算符 应该使 std::swap() 对于您的类型表现最佳。

I know you haven't asked this, but since you're using VC10, providing a move constructor and a move assignment operator should make std::swap() perform optimal for your type.

叹沉浮 2024-10-16 17:26:32

它不明确的原因是因为您提供了 ::swap 和 std::swap,然后您使用'd std::swap。现在,全局命名空间中同时拥有 ::swap 和 std::swap 。所以编译器不知道你的意思是 ::swap 还是 std::swap。

然而,提供移动操作符/构造函数将使交换以最佳、现实的方式执行。

The reason it's ambiguous is because you provided ::swap and std::swap, then you using'd std::swap. Now you have both ::swap and std::swap in the global namespace. So the compiler doesn't know if you mean ::swap or std::swap.

However, providing move operator/constructor will make swap perform optimally, realistically.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文