Chinaunix首页 | 论坛 | 博客
  • 博客访问: 903505
  • 博文数量: 158
  • 博客积分: 4380
  • 博客等级: 上校
  • 技术积分: 2367
  • 用 户 组: 普通用户
  • 注册时间: 2006-09-21 10:45
文章分类

全部博文(158)

文章存档

2012年(158)

我的朋友

分类: C/C++

2012-11-26 15:48:58

struct foo
{
    operator int*() const
    {
        return 0;
    }
    foo operator+ (int) const
    {
        return *this;
    }
};

int main()
{
    foo() + 0u;

    return 0;
}
可以编译通过

struct foo
{
    operator int*() const
    {
        return 0;
    }
    foo operator+ (unsigned) const
    {
        return *this;
    }
};

int main()
{
    foo() + 0;

    return 0;
}
在VC++2008中报错
error C2666: 2 overloads have similar conversions
could be 'foo foo::operator +(unsigned int) const'
or       'built-in C++ operator+(int *, int)'

在GCC4.5中有警告
warning: ISO C++ says that these are ambiguous, even though the worst conversion for the first is better than the worst conversion for the second:
candidate 1: foo foo::operator+(unsigned int) const
candidate 2: operator+(int*, int)

阅读(1096) | 评论(2) | 转发(0) |
给主人留下些什么吧!~~

网友评论2012-11-26 15:49:37

Anana
其中例子是operator[] 跟operator int*共存时参数的signed/unsigned问题,如果没记错的话:)

网友评论2012-11-26 15:49:29

Anana
请参考Imperfect C++关于这个问题的讨论.