Article View: comp.std.c++
Article #3034Worrying STL problem
From: David Byrden
Date: Thu, 18 Jan 1996 02:35
Date: Thu, 18 Jan 1996 02:35
57 lines
2499 bytes
2499 bytes
I have run into a problem with the STL and Visual C++ 4, but I'm
not sure whether the C++ language definition, or the STL
definition, or just Visual C++ itself is to blame.
I want to make a map of objects and key them on addresses. That's
not an unreasonable request; a map of memory blocks keyed on their
own addresses would be very useful if you wanted to concatenate them.
Here is a map declaration, using chars keyed on float addresses:
map< float*, char, less<float*> > theMap ;
Of course, when Microsoft get around to default template parameters,
I will be able to omit the functor less<float*>
Now, let's use one of the member functions of map, to insert
a char and its keying address; any address will illustrate
void f()
{
float ff ;
float* const pf = &ff ;
theMap.insert( make_pair( pf, 'g' ) ) ;
}
This won't compile! The problem? the float address is not const, but
the code in the map template requires a const key object, i.e. it
requires pair< float* const, char >
I have made pf a const float pointer, but that should not make a
difference in any case as we are passing a value.
The first problem arises with the library function make_pair(). It
takes const references to the objects you pass, but it returns a
pair<> object containing NON-CONST ones. So, is the STL at fault?
The second problem arises with the compiler. Visual C++ will not
convert pair< float* , char >
to the required pair< float* const, char >
That sounds reasonable to me, but again, is the compiler at fault?
Or does the definition of C++ ban that conversion, and of so,
is IT right to do so?
How is it SUPPOSED to work? Any relevant thoughts would be appreciated.
David
---
[ comp.std.c++ is moderated. Submission address: std-c++@ncar.ucar.edu.
Contact address: std-c++-request@ncar.ucar.edu. The moderation policy
is summarized in http://dogbert.lbl.gov/~matt/std-c++/policy.html. ]
Technical Details
Message-ID:
<4djps4$4h@news.bridge.net>Path:
arc1-ca.pugleaf.net!in1.uu.net!bounce-backShow Full Headers
Complete Article Headers
From: David Byrden <100101.2547@compuserve.com> Message-ID: <4djps4$4h@news.bridge.net> X-Original-Date: 17 Jan 1996 21:31:48 GMT Path: arc1-ca.pugleaf.net!in1.uu.net!bounce-back Date: 18 Jan 96 02:35:04 GMT Approved: fjh@cs.mu.oz.au Newsgroups: comp.std.c++ Subject: Worrying STL problem Organization: self-employed X-Mailer: Mozilla 1.1N (Windows; I; 16bit) X-Auth: PGPMoose V1.1 PGP comp.std.c++ iQBFAgUBMP2x6eEDnX0m9pzZAQHWXAF9GAC1ch53Ztf9HJJsVnFIzNRzmZlS6UmT /f777ZFs6SsjQHFa1/6Xy+PsDLMG1YVv =rjOi