Prev: Why is the return type of count_if() "signed" rather than "unsigned"?
Next: Accessor Functions (getter) for C String (Character Array) Members
From: Timothy Madden on 24 Jun 2010 09:50 Hello My compiler complains if I try to declare std::map<unsigned, RECT const &>::iterator it in the argument list for a function declaration. So I would like to know if it is legal to declare such a container. I understand that operator [] can not possibly work on such a map, because the operator creates a new pair when the requested index is not found in the map, and there is no way to initialize the enclosed refrence, but otherwise I think the container should be legal, despite my compiler's error message when declaring the iterator. Thank you, Timothy Madden -- [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ]
From: Goran on 24 Jun 2010 17:54 On Jun 25, 2:50 am, Timothy Madden <terminato...(a)gmail.com> wrote: > Hello > > My compiler complains if I try to declare > > std::map<unsigned, RECT const &>::iterator it > > in the argument list for a function declaration. > > So I would like to know if it is legal to declare such a container. I don't think so. The problem is, that would produce "references to references" in quite a bit of places. So pointers are in order. You might want to wrap the pointer in a reference-like object and use that in your map ? ( NB: very limited usability; compiled with head-compiler and tested with head-debugger ;-) ) class rect_holder { public: rect_holder(const RECT& p) :_p(&p) {} operator const RECT&() const { assert(p); return *p; } private: const RECT* _p; }; std::map<unsigned, const rect_holder> mmm; rect_holder gives you "copyability", and you can even do e.g. const RECT& r = your_map[1]; But note also: with a map, if your value type is not const, you can't use operator[] like so: your_map[key] = value; So IMO it's not a good idea to use const value type. You should instead pass const map around. Goran. -- [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ]
From: T. Schroeder on 24 Jun 2010 18:01 Am 25.06.2010 02:50, schrieb Timothy Madden: > Hello > > My compiler complains if I try to declare > > std::map<unsigned, RECT const &>::iterator it > > in the argument list for a function declaration. > ... no it isnt allowed... but you can use a boost::reference_wrapper typedef boost::reference_wrapper<RECT> rect_ref; typedef std::map<unsigned, rect_ref> rectmap; RECT r; rectmap m; m.insert(rectmap::value_type(0, boost::ref(r))); -- [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ]
From: red floyd on 25 Jun 2010 17:24
On Jun 25, 2:01 am, "T. Schroeder" <schroe...(a)ipe-chemnitz.de> wrote: > Am 25.06.2010 02:50, schrieb Timothy Madden: > > > Hello > > > My compiler complains if I try to declare > > > std::map<unsigned, RECT const &>::iterator it > > > in the argument list for a function declaration. > > ... > > no it isnt allowed... but you can use a boost::reference_wrapper > > typedef boost::reference_wrapper<RECT> rect_ref; > typedef std::map<unsigned, rect_ref> rectmap; > > RECT r; > rectmap m; > m.insert(rectmap::value_type(0, boost::ref(r))); Note that even if references to references weren't an issue, the OP's container would be invalid, because it doesn't meet the requirements in 23.1/4 that the type T stored in a container (in this case, std::pair<const unsigned, const RECT&>) be Assignable. -- [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ] |