|
|
|
#1 |
|
Messages: n/a
Hébergeur: |
Should I be able to do something like this:
static const std::map<RequestType, string> RequestMap = { { AOpenEx, "AOpenEx" }, { AClose, "AClose" }, { ARegister, "ARegister" }, { AUnregister, "Unregister" } }; RequestType is an enum. I can't compile it. Or can I not initialise with an array like this? |
|
|
|
#2 |
|
Messages: n/a
Hébergeur: |
On Fri, 27 Jun 2008 15:06:09 +0200, Angus <anguscomber@gmail.com> wrote:
> Should I be able to do something like this: > > static const std::map<RequestType, string> RequestMap = { > { AOpenEx, "AOpenEx" }, > { AClose, "AClose" }, > { ARegister, "ARegister" }, > { AUnregister, "Unregister" } }; > > RequestType is an enum. > > I can't compile it. Or can I not initialise with an array like this? look at boost::assign and particularly at map_list_of |
|
|
|
#3 |
|
Messages: n/a
Hébergeur: |
On 27 Jun, 14:13, David Côme <davidc...@wanadoo.fr> wrote:
> On Fri, 27 Jun 2008 15:06:09 +0200, Angus <anguscom...@gmail.com> wrote: > > Should I be able to do something like this: > > > static const std::map<RequestType, string> RequestMap = { > > { AOpenEx, "AOpenEx" }, > > { AClose, "AClose" }, > > { ARegister, "ARegister" }, > > { AUnregister, "Unregister" } }; > > > RequestType is an enum. > > > I can't compile it. Or can I not initialise with an array like this? > > look at boost::assign and particularly at map_list_of Unfortunately I cannot use boost here. Maybe it is easier not to use a map as the data is fairly static. What is the easiest way for me to create a mapping like this? |
|
|
|
#4 |
|
Messages: n/a
Hébergeur: |
On Jun 27, 4:30pm, Angus <anguscom...@gmail.com> wrote:
> On 27 Jun, 14:13, David Côme <davidc...@wanadoo.fr> wrote: > > > On Fri, 27 Jun 2008 15:06:09 +0200, Angus <anguscom...@gmail.com> wrote: > > > Should I be able to do something like this: > > > > static const std::map<RequestType, string> RequestMap = { > > > { AOpenEx, "AOpenEx" }, > > > { AClose, "AClose" }, > > > { ARegister, "ARegister" }, > > > { AUnregister, "Unregister" } }; > > > > RequestType is an enum. > > > > I can't compile it. Or can I not initialise with an array like this? > > > look at boost::assign and particularly at map_list_of > > Unfortunately I cannot use boost here. Maybe it is easier not to use > a map as the data is fairly static. What is the easiest way for me to > create a mapping like this? Maybe use a loop? RequestType keys[] = {AOpenEx, AClose, ARegister, AUnregister,}; const char* vals[] = {"AOpenEx", "AClose", "ARegister", "AUnregister"}; std::map<RequestType, string> myMap; for(int n = 0; n < (sizeof(keys) / sizeof(keys[0])); ++n) { myMap[keys[n]] = vals[n]; } .... If you are always going to have the string exactly the same as RequestType enum and you have a large number of such, maybe you can do some preprocessor stuff, this will avoid any errors while typing. Vivek |
|
|
|
#5 |
|
Messages: n/a
Hébergeur: |
On Jun 27, 9:30 am, Angus <anguscom...@gmail.com> wrote:
> On 27 Jun, 14:13, David Côme <davidc...@wanadoo.fr> wrote: > > > On Fri, 27 Jun 2008 15:06:09 +0200, Angus <anguscom...@gmail.com> wrote: > > > Should I be able to do something like this: > > > > static const std::map<RequestType, string> RequestMap = { > > > { AOpenEx, "AOpenEx" }, > > > { AClose, "AClose" }, > > > { ARegister, "ARegister" }, > > > { AUnregister, "Unregister" } }; > > > > RequestType is an enum. > > > > I can't compile it. Or can I not initialise with an array like this? > > > look at boost::assign and particularly at map_list_of > > Unfortunately I cannot use boost here. Maybe it is easier not to use > a map as the data is fairly static. What is the easiest way for me to > create a mapping like this? How about: typedef std::map<RequestType, string> RequestMap; RequestMap tmp_map; tmp_map[AOpenEx] = "AOpenEx"; tmp_map[AClose] = "AClose"; tmp_map[ARegister] = "ARegister"; tmp_map[AUnregister] = "AUnregister"; static const RequestMap const_map(tmp_map); A bit ugly, but it works. You might also be interested in this proposal for future C++ initializer lists: http://anubis.dkuug.dk/jtc1/sc22/wg2...2003/n1509.pdf Sean |
|
|
|
#6 |
|
Messages: n/a
Hébergeur: |
On Jun 27, 9:06am, Angus <anguscom...@gmail.com> wrote:
> Should I be able to do something like this: > > static const std::map<RequestType, string> RequestMap = { > { AOpenEx, "AOpenEx" }, > { AClose, "AClose" }, > { ARegister, "ARegister" }, > { AUnregister, "Unregister" } }; > > RequestType is an enum. > > I can't compile it. Or can I not initialise with an array like this? If your map contents won't change much in the future, just initialize it from two arrays or an array of POD structs. If the map will be added to in the future, you may want to look into having a register function. See Modern C++ Design for how to do this. In that book, he used this technique to register names/creation function pointers with a factory. HTH |
|
|
|
#7 |
|
Messages: n/a
Hébergeur: |
On Jun 27, 2:06pm, Angus <anguscom...@gmail.com> wrote:
> Should I be able to do something like this: > > static const std::map<RequestType, string> RequestMap = { > { AOpenEx, "AOpenEx" }, > { AClose, "AClose" }, > { ARegister, "ARegister" }, > { AUnregister, "Unregister" } }; > > RequestType is an enum. Check this out (a previous thread on the same question): http://groups.google.com/group/comp....a984af51eba51a |
|
|
|
#8 |
|
Messages: n/a
Hébergeur: |
Angus wrote:
> static const std::map<RequestType, string> RequestMap = { > { AOpenEx, "AOpenEx" }, > { AClose, "AClose" }, > { ARegister, "ARegister" }, > { AUnregister, "Unregister" } }; Followup question: Will this be possible with the upcoming C++ standard? (AFAIK something similar will be possible with std::vector.) |
|
![]() |
| Outils de la discussion | |
|
|