次のコードを検討してください。
#include<iostream>
#include<vector>
using namespace std;
class Foo {
public:
template< typename T>
operator vector< T >() const {
return vector< T >();
}
template< typename T>
operator T() const {
return T();
}
};
int main () {
Foo b;
vector< int > q = b;
q = b;
}
次の2つのコマンドのいずれかを使用して、Clangまたはg++でこれをコンパイルします。
g++ test.cpp
clang++ test.cpp
ただし、C ++ 11機能を有効にすると、失敗します。
g++ --std=c++0x test.cpp
clang++ --std=c++11 test.cpp
エラーメッセージは次のようになります。
test.cpp:20:5: error: use of overloaded operator '=' is ambiguous (with operand types 'vector<int>' and 'Foo')
q = b;
~ ^ ~
/usr/include/c++/4.6/bits/stl_vector.h:373:7: note: candidate function
operator=(vector&& __x)
^
/usr/include/c++/4.6/bits/stl_vector.h:362:7: note: candidate function
operator=(const vector& __x);
^
/usr/include/c++/4.6/bits/stl_vector.h:394:7: note: candidate function
operator=(initializer_list<value_type> __l)
^
1 error generated.
C ++ 11がないと機能するのに、C++11がないと機能する理由はわかりません。移動、行に注意してください
vector< int > q = b; // In the main function, line 19
main関数でエラーが発生することはありません。なぜそれが機能しないのか、そしてそれをC ++ 11で機能させるために何ができるのかを誰かが説明できますか?