【发布时间】:2012-10-24 18:33:08
【问题描述】:
考虑以下代码:
#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;
}
使用 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 的情况下也能工作,而它却失败了。 Moveover,注意那行
vector< int > q = b; // In the main function, line 19
在 main 函数中不会导致错误。谁能解释为什么它不起作用,以及如何使它与 C++11 一起工作?
【问题讨论】:
-
看来这可能是 LLVM 3.0 中的一个错误。使用 LLVM 3.1 编译它可以工作。
-
@Xeo,就这样吧。我也确信这是错误。但是,它似乎仍然是 Clang 3.1 中的一部分,因为 q = vector(b);在 q = b 有效时失败。
-
别在意我的最后一条评论,我弄混了一些东西。现在重新考虑。