Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is a re-constructed map type different from the original one?

Tags:

c++

types

Consider the following code:

#include <iostream>
#include <typeinfo>
#include <map>


int main(int argc, const char* argv[]) {
    typedef std::map<int,float> testmap;
    typedef std::map<int,float> testmap2;
    typedef std::map<typename testmap::value_type::first_type, typename testmap::value_type::second_type> rebuiltMap;
    std::cout << "map samenes: " << std::is_same<testmap, rebuiltMap>::value << "\n";
    std::cout << "map samenes: " << std::is_same<testmap, testmap2>::value << "\n";
    std::cout << "original map type name " << typeid(testmap).name() << "\n";
    std::cout << "same     map type name " << typeid(testmap2).name() << "\n";
    std::cout << "rebuilt  map type name " << typeid(rebuiltMap).name() << "\n";
    std::cout << "original map valuetype " << typeid(testmap::value_type).name() << "\n";
    std::cout << "rebuilt  map valuetype " << typeid(rebuiltMap::value_type).name() << "\n";
}

This produces the following output:

map samenes: 0
map samenes: 1
original map type name St3mapIifSt4lessIiESaISt4pairIKifEEE
same     map type name St3mapIifSt4lessIiESaISt4pairIKifEEE
rebuilt  map type name St3mapIKifSt4lessIS0_ESaISt4pairIS0_fEEE
original map valuetype St4pairIKifE
rebuilt  map valuetype St4pairIKifE

Why is the "rebuilt" map type different from the simple map type, although both have the same value_type? Background: i want to test if a container containing pairs is a map or not with a construct like

std::is_same<std::map<typename Container::value_type::first_type,
                      typename Container::value_type::second_type>,
             Container>::value
like image 809
Dr. Jürgen Hannappel Avatar asked Nov 15 '19 15:11

Dr. Jürgen Hannappel


People also ask

Why do we use so many map types?

those which describe ⁄ comment on specific features using the landscape as a background or for context (all other maps – usually called thematic maps). For ease of describing functionally quite different maps; and to explain what can be confusing differences; we have opted to use a greater number of map types.

Is a thematic map the same as a general reference map?

A thematic map is not the same as a general reference map since these maps don’t just show natural features like rivers, cities, political subdivisions, elevation, and highways.

What does a road map look like?

These maps show minor and major highways and roads (based on detail) along with things like airports, city locations, and interesting attractions, such as, parks, campgrounds, and monuments. Major highways with a road map are usually red and bigger than other roads, while minor roads can be a lighter color and a smaller line.

What are the two main types of maps?

One common point of view is that there are two main types of maps: those which summarise the actual landscape (topographic and general reference maps); and; those which describe ⁄ comment on specific features using the landscape as a background or for context (all other maps – usually called thematic maps).


2 Answers

This is because std::map::value_type is std::pair<const Key, Value>, not std::pair<Key, Value>. The reason for that is you are never allowed to modify the key of a pair that is in the map.

To get the rebuilt map to be the same using value_type::first_type you need to remove that const using std::remove_const_t like

typedef std::map<std::remove_const_t<typename testmap::value_type::first_type>, typename testmap::value_type::second_type> rebuiltMap;
like image 158
NathanOliver Avatar answered Nov 14 '22 22:11

NathanOliver


testmap::value_type::first_type is const int rather than int.

You could use testmap::key_type instead to get the correct type. Based on the pair type, it is not possible to distinguish const and non-const key type. That said, a const key would not satisfy the requirements of std::map, so you could simply assume that it is non-const and remove const from testmap::value_type::first_type.

like image 34
eerorika Avatar answered Nov 14 '22 23:11

eerorika