5

このプログラムは次を使用してコンパイルされません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&)ですか?

さらに、(上記のプログラムの)while とdo notのA a = b;両方がコンパイルされるのはなぜでしょうか。A a; a = b;A a(b);

4

1 に答える 1

4

これがどのようなバグなのかはわかりませんが、テストしている Clang のバージョンは、特に C++11 機能に関してはかなり古いものです。少なくとも、この AFAIK を正しく受け入れるClang の 3.0 リリースを使用することをお勧めします。Clang SVN トランクの最近のリビジョンでテストしたところ、問題なく動作しました。

Clang の C++11 サポートはまだ活発に開発されているため、3.0 リリースにバグがあったとしても驚かないでください。SVN トランクから直接ビルドすると、より成功する可能性があります。Subversion からコードをチェックアウトし、Clang バイナリの新しいセットを構築するための手順がここにあります。

于 2012-01-01T21:52:32.300 に答える