VC++ 6 to VS 2005 Porting problem.
-
I'm porting a legacy app. This code was fine in VC6, but VS2005 doesn't like it: struct CColumnBool { std::vector m_vBoolData; CColumnBool() { m_vBoolData.clear(); }; bool& operator[](UINT uiRow) { return m_vBoolData[uiRow]; // <--- error on this line... }; const bool& operator[](UINT uiRow) const { return m_vBoolData[uiRow]; }; }; The error is this: error C2440: 'return' : cannot convert from 'std::_Vb_reference<_MycontTy>' to 'bool &' with [ _MycontTy=std::vector> ] The weird thing is that this code does NOT generate the same error: struct CColumnInt { std::vector m_vIntData; CColumnInt() { m_vIntData.clear(); }; int& operator[](UINT uiRow) { return m_vIntData[uiRow]; }; const int& operator[](UINT uiRow) const { return m_vIntData[uiRow]; }; }; C2440 is a type conversion error, of course. MSDN lists several causes for this, but I don't see where they apply. Any ideas how to fix this?
-
I'm porting a legacy app. This code was fine in VC6, but VS2005 doesn't like it: struct CColumnBool { std::vector m_vBoolData; CColumnBool() { m_vBoolData.clear(); }; bool& operator[](UINT uiRow) { return m_vBoolData[uiRow]; // <--- error on this line... }; const bool& operator[](UINT uiRow) const { return m_vBoolData[uiRow]; }; }; The error is this: error C2440: 'return' : cannot convert from 'std::_Vb_reference<_MycontTy>' to 'bool &' with [ _MycontTy=std::vector> ] The weird thing is that this code does NOT generate the same error: struct CColumnInt { std::vector m_vIntData; CColumnInt() { m_vIntData.clear(); }; int& operator[](UINT uiRow) { return m_vIntData[uiRow]; }; const int& operator[](UINT uiRow) const { return m_vIntData[uiRow]; }; }; C2440 is a type conversion error, of course. MSDN lists several causes for this, but I don't see where they apply. Any ideas how to fix this?
-
This may help perhaps: http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=1354391&SiteID=1[^]
Adis H. wrote:
This may help perhaps: http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=1354391&SiteID=1\[^\]
Yes, that's it exactly. Thanks! :-D
-
I'm porting a legacy app. This code was fine in VC6, but VS2005 doesn't like it: struct CColumnBool { std::vector m_vBoolData; CColumnBool() { m_vBoolData.clear(); }; bool& operator[](UINT uiRow) { return m_vBoolData[uiRow]; // <--- error on this line... }; const bool& operator[](UINT uiRow) const { return m_vBoolData[uiRow]; }; }; The error is this: error C2440: 'return' : cannot convert from 'std::_Vb_reference<_MycontTy>' to 'bool &' with [ _MycontTy=std::vector> ] The weird thing is that this code does NOT generate the same error: struct CColumnInt { std::vector m_vIntData; CColumnInt() { m_vIntData.clear(); }; int& operator[](UINT uiRow) { return m_vIntData[uiRow]; }; const int& operator[](UINT uiRow) const { return m_vIntData[uiRow]; }; }; C2440 is a type conversion error, of course. MSDN lists several causes for this, but I don't see where they apply. Any ideas how to fix this?
std::vector<bool>
is not based on an array ofbool
. Instead the bits are packed, and as such it is impossible to take the address of any given bit, or create a reference to it. Insteadoperator[]
has to return a proxy class. This is a specialization of thevector
template required by the C++ standard. This is widely considered to be a massive error in the standard. Unfortunately it is required, in order to claim conformance to the standard.Stability. What an interesting concept. -- Chris Maunder
-
std::vector<bool>
is not based on an array ofbool
. Instead the bits are packed, and as such it is impossible to take the address of any given bit, or create a reference to it. Insteadoperator[]
has to return a proxy class. This is a specialization of thevector
template required by the C++ standard. This is widely considered to be a massive error in the standard. Unfortunately it is required, in order to claim conformance to the standard.Stability. What an interesting concept. -- Chris Maunder