การใช้ Visual Studio 2010 SP1:
#include <vector>
//namespace XXX {
struct Test
{
bool operator==(const Test& r) const { return true; }
};
//}
//typedef XXX::Test Test;
template <typename T> inline bool operator!=(const T& l,const T& r)
{ return !(l==r); }
int main()
{
std::vector<Test> vt;
std::vector<Test> vt2 = std::move(vt);
return 0;
}
หากฉันคอมไพล์โค้ดด้านบนตามที่เป็นอยู่ มันจะล้มเหลวโดยมีข้อผิดพลาดนี้:
1>C:\apps\MVS10\VC\include\vector(609): error C2593: 'operator !=' is ambiguous
1> C:\apps\MVS10\VC\include\xmemory(268): could be 'bool std::operator !=<_Ty,_Ty>(const std::allocator<_Ty> &,const std::allocator<_Ty> &) throw()'
1> with
1> [
1> _Ty=Test
1> ]
1> test.cpp(11): or 'bool operator !=<std::allocator<_Ty>>(const T &,const T &)' [found using argument-dependent lookup]
1> with
1> [
1> _Ty=Test,
1> T=std::allocator<Test>
1> ]
1> while trying to match the argument list '(std::allocator<_Ty>, std::allocator<_Ty>)'
1> with
1> [
1> _Ty=Test
1> ]
1> C:\apps\MVS10\VC\include\vector(606) : while compiling class template member function 'void std::vector<_Ty>::_Assign_rv(std::vector<_Ty> &&)'
1> with
1> [
1> _Ty=Test
1> ]
... โดยที่ vector(609)
แก้ไขเป็นบรรทัดนี้:
else if (get_allocator() != _Right.get_allocator())
OTOH หากฉันไม่ใส่ข้อคิดเห็นบรรทัดที่เกี่ยวข้องกับ namespace XXX
มันจะรวบรวมโดยไม่มีการร้องเรียน
ฉันต้องคิดว่านี่เป็นข้อบกพร่องของคอมไพเลอร์ แต่ฉันกำลังมองหาการตรวจสอบอิสระ
แก้ไข: จากการอธิบาย ฉันพบสถานการณ์นี้เมื่อคอมไพล์โค้ดเก่าด้วย VS2010 เป็นครั้งแรก ผู้ดำเนินการทั่วโลกมีปัญหาบางอย่างเมื่อหลายปีก่อน (ตอนนี้ถูกลบออกแล้ว) ฉันไม่เข้าใจว่าทำไมโค้ดบางอันจึงล้มเหลวและบางอันก็ไม่เข้าใจ โค้ดด้านบนเป็นการกลั่นกรองกรณีที่ล้มเหลวของฉัน (เห็นได้ชัดว่าโค้ดเก่าจะไม่มีการเรียกไปยัง std::move()
)
อัปเดต: ฉันบันทึกจุดบกพร่องด้วย MS และพวกเขาตอบว่าสิ่งนี้ได้รับการแก้ไขแล้ว "ในคอมไพเลอร์รุ่นถัดไป" ซึ่งฉันคิดว่าหมายถึง Visual C++ 11 ดู: http://connect.microsoft.com/VisualStudio/feedback/details/731692/regression-involving-global-operator-and-std-vector
vt=f()
) บน GCC รุ่นเก่าใช้งานได้ แต่เห็นได้ชัดว่านั่นไม่เทียบเท่ากับเวอร์ชันที่รองรับ rvalue-refs อย่างแม่นยำ หลังจากนั้นฉันก็กลั่นมันลงไปที่ std::move() - person mcmcc   schedule 16.03.2012