代碼:在std :: reference_wrapper中的Visual C++ 10.0錯誤?
#include <functional>
struct Foo
{
virtual void mf() = 0;
};
struct Bar: Foo
{
virtual void mf() {}
};
int main()
{
Bar o;
std::reference_wrapper<Foo const> wrapper(o);
}
結果使用MinGW克++ 4.6.1:
[d:\dev\test] >g++ foo.cpp -std=c++0x [d:\dev\test] > _
結果用Visual C++ 10.0:
[d:\dev\test] >cl foo.cpp foo.cpp C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\INCLUDE\xxcallwrap(8) : error C2259: 'Foo' : cannot instantiate abstract class due to following members: 'void Foo::mf(void)' : is abstract foo.cpp(5) : see declaration of 'Foo::mf' C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\INCLUDE\xrefwrap(371) : see reference to class template instantiation 'std::tr1::_Call_wrapper' being compiled with [ _Callable=std::tr1::_Callable_obj ] C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\INCLUDE\xrefwrap(416) : see reference to class template instantiation 'std::tr1::_Refwrap_impl' being compiled with [ _Ty=const Foo ] foo.cpp(16) : see reference to class template instantiation 'std::tr1::reference_wrapper' being compiled with [ _Ty=const Foo ] C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\INCLUDE\xxcallwrap(8) : error C2027: use of undefined type 'std:: tr1::_Result_of' with [ _Ty=const Foo (void) ] C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\INCLUDE\xxcallwrap(9) : error C2143: syntax error : missing ';' before '(' C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\INCLUDE\xxcallwrap(9) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\INCLUDE\xxcallwrap(10) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\INCLUDE\xxcallwrap(8) : error C2259: 'Foo' : cannot instantiate abstract class due to following members: 'void Foo::mf(void)' : is abstract foo.cpp(5) : see declaration of 'Foo::mf' C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\INCLUDE\xxcallwrap(8) : error C2259: 'Foo' : cannot instantiate abstract class due to following members: 'void Foo::mf(void)' : is abstract foo.cpp(5) : see declaration of 'Foo::mf' C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\INCLUDE\xxcallwrap(8) : error C2259: 'Foo' : cannot instantiate abstract class due to following members: 'void Foo::mf(void)' : is abstract foo.cpp(5) : see declaration of 'Foo::mf' C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\INCLUDE\xxcallwrap(8) : error C2259: 'Foo' : cannot instantiate abstract class due to following members: 'void Foo::mf(void)' : is abstract foo.cpp(5) : see declaration of 'Foo::mf' C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\INCLUDE\xxcallwrap(8) : error C2259: 'Foo' : cannot instantiate abstract class due to following members: 'void Foo::mf(void)' : is abstract foo.cpp(5) : see declaration of 'Foo::mf' C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\INCLUDE\xxcallwrap(8) : error C2259: 'Foo' : cannot instantiate abstract class due to following members: 'void Foo::mf(void)' : is abstract foo.cpp(5) : see declaration of 'Foo::mf' C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\INCLUDE\xxcallwrap(8) : error C2259: 'Foo' : cannot instantiate abstract class due to following members: 'void Foo::mf(void)' : is abstract foo.cpp(5) : see declaration of 'Foo::mf' C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\INCLUDE\xxcallwrap(8) : error C2259: 'Foo' : cannot instantiate abstract class due to following members: 'void Foo::mf(void)' : is abstract foo.cpp(5) : see declaration of 'Foo::mf' C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\INCLUDE\xxcallwrap(8) : error C2259: 'Foo' : cannot instantiate abstract class due to following members: 'void Foo::mf(void)' : is abstract foo.cpp(5) : see declaration of 'Foo::mf' C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\INCLUDE\xxcallwrap(8) : error C2259: 'Foo' : cannot instantiate abstract class due to following members: 'void Foo::mf(void)' : is abstract foo.cpp(5) : see declaration of 'Foo::mf' [d:\dev\test] > _
我喜歡的方式,所述的視覺C++編譯器11次告訴用戶抽象類不能被實例化,只是爲了錘鍊e點家,如果用戶不知道這一點。但std::reference_wrapper
真的應該實例化類?是不是很多的(通過)參考而不是要求實例化?
也就是說,我強烈懷疑這是Visual C++標準庫實現中的一個錯誤嗎?
我提交了一個[bug報告](https://connect.microsoft.com/VisualStudio/feedback/details/727368/std-reference-wrapper-fails-to-wrap-reference-to-object-of-abstract -class)給Microsoft。 – 2012-03-01 00:03:52
您是否應該不使用ref輔助函數創建reference_wrapper? – 2012-03-01 11:41:01
@jamesj:沒有。這只是一個幫助函數,以避免明確指定類型。爲了調查一個bug,明確的事情比隱含的事情更好。 – 2012-03-01 14:32:22