Thread View: comp.std.c++
1 messages
1 total messages
Started by David Byrden
Thu, 18 Jan 1996 02:35
Worrying STL problem
Author: 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. ]
Thread Navigation
This is a paginated view of messages in the thread with full content displayed inline.
Messages are displayed in chronological order, with the original post highlighted in green.
Use pagination controls to navigate through all messages in large threads.
Back to All Threads