【问题标题】:Compile error when calling a move overloaded function with an implicitly convertible object使用隐式可转换对象调用移动重载函数时出现编译错误
【发布时间】:2012-01-30 17:51:07
【问题描述】:

这个程序不能使用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); 都不能编译?

【问题讨论】:

  • FWIW,您的代码使用 clang version 3.0 (tags/RELEASE_30/final) Target: x86_64-pc-linux-gnu Thread model: posix 和 GCC 4.5.3 或 4.6.2 按原样编译(但我不知道这是否正常)

标签: c++ c++11 overloading clang move-semantics


【解决方案1】:

我不确定这是什么错误,但您正在测试的 Clang 版本相当旧,尤其是在 C++11 功能方面。您可能至少希望使用正确接受此 AFAIK 的3.0 release of Clang。我用 Clang SVN 主干的最新版本对其进行了测试,它运行良好。

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-08-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多