使用隐式可转换对象调用移动重载函数时出现编译错误

发布于 2024-12-23 12:44:34 字数 1395 浏览 4 评论 0原文

该程序无法使用clang++ test.cpp -std=c++0x进行编译:

class A
{
public:
    A() {}
    A(const A&) {}
    A(A&&) {}
    A& operator = (const A&) { return *this; }
    A& operator = (A&&) { return *this; }
};

class B
{
    A m_a;
public:
    operator const A &() const
    {
        return m_a;
    }
};

int main(int, char**)
{
    A a;
    B b;
    a = b; // compile error
}

编译错误:

Apple clang version 3.0 (tags/Apple/clang-211.10.1) (based on LLVM 3.0svn)

test.cpp:25:9: error: no viable conversion from 'B' to 'A'
    a = b;
        ^
test.cpp:5:5: note: candidate constructor not viable: no known conversion from 'B' to
      'const A &' for 1st argument
    A(const A&) {}
    ^
test.cpp:6:5: note: candidate constructor not viable: no known conversion from 'B' to 'A &&'
      for 1st argument
    A(A&&) {}
    ^
test.cpp:15:5: note: candidate function
    operator const A &() const
    ^
test.cpp:8:23: note: passing argument to parameter here
    A& operator = (A&&) { return *this; }
                      ^

为什么无法编译?为什么编译器更喜欢 A::operator = (A&&) 而不是 A::operator = (const A&)

另外,为什么A a = b;会编译,而A a; a = b; (上面的程序)和 A a(b); 不是吗?

This program does not compile using clang++ test.cpp -std=c++0x:

class A
{
public:
    A() {}
    A(const A&) {}
    A(A&&) {}
    A& operator = (const A&) { return *this; }
    A& operator = (A&&) { return *this; }
};

class B
{
    A m_a;
public:
    operator const A &() const
    {
        return m_a;
    }
};

int main(int, char**)
{
    A a;
    B b;
    a = b; // compile error
}

Compile errors:

Apple clang version 3.0 (tags/Apple/clang-211.10.1) (based on LLVM 3.0svn)

test.cpp:25:9: error: no viable conversion from 'B' to 'A'
    a = b;
        ^
test.cpp:5:5: note: candidate constructor not viable: no known conversion from 'B' to
      'const A &' for 1st argument
    A(const A&) {}
    ^
test.cpp:6:5: note: candidate constructor not viable: no known conversion from 'B' to 'A &&'
      for 1st argument
    A(A&&) {}
    ^
test.cpp:15:5: note: candidate function
    operator const A &() const
    ^
test.cpp:8:23: note: passing argument to parameter here
    A& operator = (A&&) { return *this; }
                      ^

Why does it not compile? Why does the compiler prefer A::operator = (A&&) over A::operator = (const A&)?

In addition, why would A a = b; compile while both A a; a = b; (the above program) and A a(b); do not?

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

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

发布评论

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

评论(1

弥繁 2024-12-30 12:44:34

我不确定这是什么错误,但您正在测试的 Clang 版本相当旧,尤其是在 C++11 功能方面。您可能至少想使用 3.0 版本的 Clang,它正确地接受了这一点AFAIK。我用 Clang SVN trunk 的最新版本对其进行了测试,效果很好。

鉴于 Clang 的 C++11 支持仍处于非常活跃的开发阶段,如果 3.0 版本中也存在错误,请不要感到惊讶。直接从 SVN 主干进行构建可能会取得更大的成功。 此处提供了有关从 subversion 检查代码并构建一组新的 Clang 二进制文件的说明。

I'm not sure what bug this is, but the version of Clang you are testing is fairly old, especially with respect to C++11 features. You probably want to use at the very least the 3.0 release of Clang, which correctly accepts this AFAIK. I tested it with a recent revision of the Clang SVN trunk, and it worked fine.

Given that Clang's C++11 support is still under very active development, don't be surprised if there are also bugs in the 3.0 release. You may have more success with a build directly from the SVN trunk. There are instructions here for checking out the code from subversion and building a fresh set of Clang binaries.

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