Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use data type (class type) as key in a map

Tags:

c++

templates

I have class Base and classes Derived_1, Derived_2 ... I need derived classes to have an id. Those ids are used for further lookups etc, and thus need to be consecutive (no just some random numbers). Because derived classes are created by user, id can not be member of Derived_N. So I came up with DerivedType class.

class DerivedType {     static unsigned id;     unsigned m_id; public:     DerivedType() : m_id(id++) {  } } 

Now I want to create a mapping between Derived_N and DerivedType. Whenever Derived_N is created, this mapping looks if DerivedType for particular Derived_N already exist and returns it, otherwise create new and stores in the map.

Actual question: Is there any way to use std::map with data type as key in the map? I am not afraid of any template-metaprogram solution. Or is there elegant way how to achieve my goal?

edit Date type -> Data type, I mean like ClassType, I am sorry :)

I want to use it like:

Derived_5 d; DerivedType dt = getType(d); //Derived_5 is looked up in map, returning particular DerivedType dt.getId(); 

every instance of Derived_N (with same 'N') should have the same id throu DerivedType

EDIT2 - MY ANSWER I found better solution for my problem... It is like this:

atomic_counter s_nextEventClassID;  typedef int cid_t;  template<class EventClass> class EventClassID { public:     static cid_t getID()     {         static cid_t classID = EventClassID::next();         return classID;     }      static cid_t next() { return ++s_nextEventClassID; } }; 

since my question was how to use datatype in a map, I will mark some of yours answers, thank you

like image 540
relaxxx Avatar asked Mar 25 '12 10:03

relaxxx


People also ask

Can we use any user defined data type as a key in map in C++?

We can use any of the data types as the data type of the key of the map. Even a user-defined data type can be used as key data type.

Can a map be a key C++?

A C++ map is a way to store a key-value pair. A map can be declared as follows: #include <iostream> #include <map> map<int, int> sample_map; Each map entry consists of a pair: a key and a value.

Is map a class in C++?

The Map is a built-in class in the C++ standard template library. The Map properties are it store elements in sorted form based on the keys, it stores unique keys that can be added or removed but cannot be updated and values corresponding with keys can be duplicated and can be updated.

How do you use Typeid?

To use the typeid operator in a program, one needs to include the library header <typeinfo>. It returns the lvalue of type const type_info to represent the type of value. Expression of typeid is an lvalue expression (lvalue has the address which is accessible by the program.


2 Answers

C++11 solves this by providing std::type_index, in <typeindex>, which is a copyable, comparable and hashable object constructed from a std::type_info object that can be used as the key in associative containers.

(The implementation is fairly simple, so even if you don't have C++11 yourself, you could steal the implementation from, say GCC 4.7, and use that in your own code.)

#include <typeindex> #include <typeinfo> #include <unordered_map>  typedef std::unordered_map<std::type_index, int> tmap;  int main() {     tmap m;     m[typeid(main)] = 12;     m[typeid(tmap)] = 15; } 
like image 130
Kerrek SB Avatar answered Sep 24 '22 13:09

Kerrek SB


You can use typeid(object) directly, since there is type_info::before, which can be used as comparator if you use type_info as key in the map, see What is `type_info::before` useful for?. No need to get .name().

like image 20
eudoxos Avatar answered Sep 20 '22 13:09

eudoxos