下面的代碼不能在gcc 4.7中編譯,但是在VS(9,10,11)中編譯也遵循gcc輸出。gcc中的C++錯誤和成功VS
#include <iostream>
using namespace std;
class A
{
public:
virtual void M() = 0;
};
class B
{
public:
inline B& operator<<(A &value)
{
value.M();
return *this;
}
};
class C: public A
{
public:
virtual void M()
{
cout << "Hello World" << endl;
}
};
int main()
{
B b;
C c;
b << c; //line not erro
b << C(); //Line with error
return 0;
}
GCC日誌
$g++ main.cpp -o test main.cpp: In function 'int main()':
main.cpp:36:12: error: no match for 'operator<<' in 'b << C()'
main.cpp:36:12: note: candidates are: main.cpp:14:15: note: B&
B::operator<<(A&) main.cpp:14:15: note: no known conversion for
argument 1 from 'C' to 'A&'
您應該聽取MSVC向您發出的警告。在某些時候,它告訴你「臨時複製到l值引用是非標準行爲」(或類似的東西)... –