Getting Pointer or Reference to mapped_type from Arbitrary Map

Edit: I've found and written up a solution to my problem but I've left the question unanswered since my solution may still not be ideal.

I'm writing a small library designed to perform routines on maps of maps, but I'm having trouble designing a set of class templates that will let me get a pointer or reference (depending on the second_type of the map's value_type) to a map's mapped_type, regardless of the type of the map (e.g. std::map, boost::ptr_map).

To elaborate further, I have tabulated some input types and desired output types.

Case Input Type Output Type A std::map<int, std::map<int, int> > std::map<int, int>& B std::map<int, boost::ptr_map<int, int> > boost::ptr_map<int, int>& C boost::ptr_map<int, std::map<int, int> > std::map<int, int>* const D std::map<int, std::map<int, int> >* std::map<int, int>& E std::map<int, boost::ptr_map<int, int> >* boost::ptr_map<int, int>& F boost::ptr_map<int, std::map<int, int> >* std::map<int, int>* const

My code passes cases A, B, D and E, but fails on cases C and F. Here is what I have so far.

template <class Map> struct map_utils { template <class K> static typename boost::remove_pointer< typename Map::value_type >::type::second_type& get(Map& m, const K k) { return m[k]; } template <class K> static typename boost::remove_pointer< typename Map::value_type >::type::second_type& get(const Map& m, const K k) { return const_cast<Map&>(m)[k]; } }; template <class Map> struct map_utils<Map*> { template <class T> static typename boost::remove_pointer< typename Map::value_type >::type::second_type& get(Map* m, const T t) { return (*m)[t]; } template <class T> static typename boost::remove_pointer< typename Map::value_type >::type::second_type& get(const Map* m, const T t) { return const_cast<Map*>(m)->operator[](t); } };

I'm trying to use boost::mpl to do this, and this is what I've cooked up so far, but I get the same error using both versions of the code.

The error.

error: invalid initialization of reference of type ‘std::map<int, double>* const&’ from expression of type ‘boost::ptr_container_detail::reversible_ptr_container<boost::ptr_container_detail::map_config<std::map<int, double>, std::map<int, void*, std::less<int>, std::allocator<std::pair<const int, void*> > >, true>, boost::heap_clone_allocator>::Ty_’

The modified specialization of the struct to deal with l-values that are not pointers to maps.

template <class K> static typename boost::mpl::if_< boost::is_pointer< typename boost::remove_pointer< typename Map::value_type >::type::second_type >, typename boost::remove_pointer< typename boost::remove_const< typename Map::value_type >::type >::type::second_type, typename boost::remove_pointer< typename Map::value_type >::type::second_type& >::type get(Map& m, const K k) { return m[k]; }

-------------Problems Reply------------

C and F seems wrong, the mapped type isn't boost::ptr_map. Otherwise it sounds like you could just use full template specialization to decide whether it is an std::map or boost::ptr_map. Something like this:

template <class Map>
class Whatever;

template <class K, class V>
class Whatever<std::map<K, V> >
{
public:
typedef V& Type;
};

template <class K, class V>
class Whatever<std::map<K, V>* >
{
public:
typedef V& Type;
};

template <class K, class V>
class Whatever<boost::ptr_map<K, V> >
{
public:
typedef V* const Type;
};

template <class K, class V>
class Whatever<boost::ptr_map<K, V>* >
{
public:
typedef V* const Type;
};

I've got the code the compile successfully and greatly simplified it in the process, by using the mapped_type typedef like so:

template <class K>
static typename boost::remove_pointer<
typename Map::mapped_type
>::type&
get(Map& m, const K k)
{
return m[k];
}

Only the body of the methods needs to be changed in specialized versions of the struct defined in the question above in order to accommodate for pointers to maps as well. If you would like the entire code listing, let me know and I'll dump the entire thing here.

Category:c# Views:0 Time:2011-08-28

Related post

  • Reference to value of STL map element? 2009-02-15

    Is it OK to pass to function a reference to the value of map element, and to modify it there? foo(string & s) { s = "xyz"; } map<int, string> m; m[1] = "abc"; foo(m[1]); // <-- Is it ok? Will m[1] be "xyz" after this call? Thank you. ---

  • C++ Storing references to values in std::map 2009-07-01

    Am I right in assuming that adding/removing elements to an std::map does not effect the other elements (ie cause them to be relocated in memory) and so that the following is safe: I looked at various sites with info on the container but only found ou

  • Reference as key in std::map 2010-07-13

    Suppose some data structure: typedef struct { std::string s; int i; } data; If I use the field data.s as key when adding instances of data in a map of type std::map<std::string&, data>, do the string gets copied? Is it safe to erase an elem

  • How object can be stored as reference in array, list or map? 2011-08-05

    Lets take an example of array class Bird{ public: int a; }; void fn(Bird& obj){ Bird& arrayList[5]; arrayList[0] = obj; arrayList[0].a = 10; } Here any update in arrayList will not be reflected in obj object My requirement is to stop copying

  • Reference an array of Google Map markers 2011-08-18

    I am trying to reference an array of markers that I created but when I try reference an array element the element receives the message GLOBAL_MARK.0 is null or not an object. I think it might be a scope issue but I'm not sure how to handle it. Here's

  • Insert reference to pointer into an map 2012-01-31

    struct A { }; int main() { A *a = new A; std::unordered_map<int, A*&> hash; hash.insert(make_pair(1, a)); //error } What is the syntax to make this work? If I do this: a = new A; I want the hashmap to point to the new object. --------------

  • Write arbitrary map and reduce function 2011-04-16

    I want to write my own map and reduce function in mapreduce framework How can I do that??(my programming language is java) Thanks. --------------Solutions------------- Just extend from the Mapper and Reducer class and override the methods you need. S

  • Best way to map Object ID References? 2010-11-04

    I'm writing an application where I have many objects (data models) that are identified by a unique String ID that every such object possesses and these objects can refer to each other by their IDs. So far so good but now I need to keep track of which

  • How does a swing component makes a look up in its ActionMap from it's inputMap using the intermediate Object reference 2011-04-22

    I've been reading about registering the "Action" objects with multiple components, and I came across the recipe of registering (binding) an Action object to a Keystroke object. In the pursuit I came across this piece of code: InputMap imap = panel.ge

  • speed of references in C++ 2009-05-25

    I have been working on a project and trying to find the source of a large slowdown in execution time and have narrowed it down to a single method which I have managed to optimise out of the logic. The problem is that my solution involves using a refe

  • HasOne vs References Mapping Fluent NHibernate 2010-03-04

    This is the first time I am working with FluentNhibernate Mapping and facing a question of how to reference another table. Any help is appreciated: I have several tables named CD_varname and all these contain two columns - CODE and DESCR. I have one

  • How can I convert HTML character references (ף) to regular UTF-8? 2010-08-25

    I have some hebrew websites that contains character references like: &#x5E0;&#x5D5;&#x5E3; I can only view these letters if I save the file as .html and view in UTF-8 encoding. If I try to open it as a regular text file then UTF-8 encodin

  • Nhinerbate lazy loading of reference entity 2010-11-24

    I have this scenario: class A { public virtual int Id { get; set; } public virtual B Child { get; set; } } class B { public virtual int Id { get; set; } } In the mapping of class A, I have a reference to class B: map.Reference(a => a.Child).LazyLo

  • Equivalent of reference 'Copy Local' in ASP.NET Web Site project 2010-11-25

    I don't see any exact duplicates in the Related Questions above, so here goes. Please don't stone me if it is a duplicate. Is there any way to achieve the same end as setting 'Copy Local' to True on a web application reference? I could probably map a

  • JSON Serializer for arbitrary HashMaps in Voldemort 2010-12-05

    I am trying to set up the configuration for Voldemort key-value store. Now, I'd like to be able to store arbitrary hashmaps in it, but I haven't found the way to do so (or if it is possible). According to the documentation I should use this syntax: {

  • Bing Maps APIs and Microsoft.Phone.Controls.Maps ambiguous Reference 2010-12-21

    I am facing a weird issue. I dont know whether this is an issue or not. Any help will be very much appreciated. I am developing a map application. I already had a working code. Now i am trying to integrate it with another project. But when i add serv

  • ef4 cause Circular reference in web service 2011-04-23

    I have a Reason object: public class Reason { public virtual long Id { get; set; } public virtual string Name { get; set; } public virtual Company Company {get;set;} } I am using entity framework 4 and Company is navigation property to Company. I als

  • References/has-a mapping thru 3 tables 2011-05-09

    My model object Reading has a Location but it's not a direct relationship in the database. In the DB, this "has-a" relationship or "reference" spans 3 tables, as shown in this snip: My Reading maps to the ComponentReading table and i want my Location

  • Perl: Can someone explain this code? It involves map, sort, tr and references. (Modified Schwartzian Transform) 2011-05-29

    I've read tutorials and perldoc on map, tr and references, but this code is a little too advanced for a beginner Perl user like myself. print map $_->[1], sort { $a->[0] cmp $b->[0] ##first element of the array or $a->[1] cmp $b->[1] }

Copyright (C) dskims.com, All Rights Reserved.

processed in 0.094 (s). 11 q(s)