Boost logo

Boost Users :

Subject: [Boost-users] [operators] Implicit instantiation.
From: Robert Jones (robertgbjones_at_[hidden])
Date: 2009-05-13 03:54:36


I need to write a map class which distinguishes read from writing
using the subscripting notation, so I wrapped the std::map, forwarded
all the typdefs and methods, except for operator[], like this

template <
    class Key,
    class T,
    template < typename Core > class Access
> struct my_map
{
  typedef std :: map< Key, T > core_type;
  typedef Access< core_type > proxy_type;

  typedef typename core_type :: key_type key_type;
  // ...lots of other stuff...
  proxy_type operator[]( const key_type & key );
};

where a typical 'Access' policy might be something like
this.

template < class T > struct proxy
{
  typedef T map_type;
  typedef typename map_type :: key_type key_type;
  typedef typename map_type :: mapped_type mapped_type;

  proxy( map_type & core, const key_type & key );
  proxy operator = ( const mapped_type & value );
  operator mapped_type & ( ) const;

};

Ok, all that broadly works, except expressions like

my_map< string, string, proxy > m;
if ( m[ "fred" ] == "bloggs" )....

won't compile even with a bool operator==(proxy, proxy), because
the compiler can't figure out the implicit type conversions.

What's needed is

  friend bool operator == ( proxy, const mapped_type & );

inside the proxy class, which does work. However I'd also like operator!=()
to work in the same way, so I thought boost::operators, hence the
declaration of proxy becomes

template < class T > struct proxy :
  boost :: equality_comparable< proxy< T >, typename T :: mapped_type >
{ ... };

Which solves the problem.

Now, the next twist is that I'd like to be able to compare my_map elements
against
any type which I can lexically convert to the mapped type, so an operator
like

  friend template < class U > bool operator==( proxy, const U & );

but how do I express that to boost::equality_comparable, since the U type
is generated by implicit instantiation, which applies to functions but
not to classes?

Thanks for reading.

- Rob



Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net