|
|
Warp escribió:
> Nicolas Alvarez <nic### [at] gmailisthebestcom> wrote:
>> Ahh, then I can say I did figure out both.
>
> Ok, if you want a harder puzzle, here's a curious quirk about C++ (not
> related to memory leaks, though):
>
> //------------------------------------------------------------------------
> #include <map>
>
> struct A { int i; };
>
> struct Comp
> { bool operator()(const A& a1, const A& a2) const { return a1.i < a2.i; }
> };
>
> int main()
> {
> std::map<A, int, Comp> theMap(Comp());
> A a = { 1 };
> theMap[a] = 5;
> }
Hrm, I never used map myself. I think I'll go search documentation on
what the template parameters are.
> a) This doesn't compile. Without using a compiler, can you tell where
> is the problem and what is the exact reason why it doesn't compile?
>
> b) If you can't see it, try it with a compiler. I bet you still can't see
> the problem... :P
>
> c) In the erroneous line, can you tell me exactly what that line means?
>
> d) What is the easiest way (smallest amount of extra code) to make it
> compile and work as expected?
Oh I think I know. I took a while without noticing anything wrong, but
now I remember having read about it in C++ FAQ Lite. theMap(Comp())
*declares a function* instead of instantiating an object. Good to know
all the time I wast^H^H^H^H spent reading that website was actually useful.
Okay, *after* remembering that and writing that paragraph, I went and
searched for what I remembered having read. Maybe that counts as
cheating... I wouldn't have searched if I had read question D before, so
I could try figuring out the solution myself :)
http://www.parashift.com/c++-faq-lite/ctors.html#faq-10.2
The solution is:
std::map<A, int, Comp> theMap = Comp();
Post a reply to this message
|
|