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

Dav*_*Lin 5 c++ overloading clang move-semantics c++11

该程序不使用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
}
Run Code Online (Sandbox Code Playgroud)

编译错误:

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; }
                      ^
Run Code Online (Sandbox Code Playgroud)

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

另外,为什么会A a = b;同时编译A a; a = b;(上述程序)而A a(b);不是?

Cha*_*uth 4

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

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