Re: Using non-const predicate in std::map fails to compile in release
??????MS???parport IOCTL wrote:
#include <iostream>
#include <functional>
#include <map>
#include <cassert>
#include <tchar.h>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
struct c_wstring_compare
: std::binary_function<const wchar_t *, const wchar_t *, bool>
{
inline result_type
operator()(first_argument_type left, second_argument_type right)
{
return wcscmp(left, right) < 0;
}
};
typedef map<const wchar_t *, const wchar_t *, c_wstring_compare>
MyCharMap;
MyCharMap container;
container.find(_T("key2"));
return 0;
}
The code above can be compiled successfully in debug mode while fails in
release mode (visual studio 2008).
The error message is:
E:\Microsoft Visual Studio 9.0\VC\include\xtree(1268) : error C3848:
expression having type 'const wmain::c_wstring_compare' would lose some
const-volatile qualifiers in order to call 'bool
wmain::c_wstring_compare::operator ()(const wchar_t *,const wchar_t *)'
Should it be regarded as a defect?
Jim:
After some hunting in the STL headers, I find that the reason why there is a
difference between debug and release mode is that the STL code in <xtree> (and
other places) uses the macro function _DEBUG_LT_PRED().
In release mode this is defined as
#define _DEBUG_LT_PRED(pred, x, y) pred(x, y)
so it requires pred to be non-const if the comparison operator is non-const.
But in debug mode it is defined as
#define _DEBUG_LT_PRED(pred, x, y) _DEBUG_LT_PRED_IMPL(pred, x, y, __FILEW__,
__LINE__)
#define _DEBUG_LT_PRED_IMPL _Debug_lt_pred
where _Debug_lt_pred() is a template function that takes its first argument by
value, and therefore pred can be const, even if the comparison operator is
non-const.
IMHO, you should always define the comparison operator to be const, and then
this problem would not arise. But the debug and release versions would compile
consistently if _Debug_lt_pred() took its first argument by non-const reference.
They would both fail if the comparison operator were non-const.
--
David Wilkinson
Visual C++ MVP