Boost logo

Boost :

From: Mark Schlegel (moschleg_at_[hidden])
Date: 2007-09-24 12:48:28


This is my multi_index code for the type that goes in the container (MapEntry<Keyt, T>)and the container definition "AgedMap"

-------------------
struct keytag { };
struct agetag { };

template <typename KEYT, typename T>
struct MapEntry
{
    MapEntry(KEYT key_, int age_, T* ptr) : key(key_), age(age_)
    {
        payload.reset(ptr);
    }

    ~MapEntry()
    {
        //cout << "destructing entry for key " << key << endl;
    }

    friend std::ostream& operator<<(std::ostream& os, const MapEntry<KEYT,T>& am)
    {
        os << "key: " << am.key << " " << " age: " << am.age << " " << std::endl;
        return os;
    }
    friend int getAge(const MapEntry<KEYT,T>& am) { return am.age; }
    friend KEYT getKey(const MapEntry<KEYT,T>& am) { return am.key; }
    T* getPayload() const { return payload.get(); }

    //void setKey(int key_) { key = key_; }
    void setAge(int age_) { age = age_; }

    KEYT key;
    int age;
  private:
    boost::shared_ptr<T> payload;
};

template<typename KEYT, typename T>
class AgedMap {
    public:
        typedef multi_index_container<
               MapEntry<KEYT, T>,
                    indexed_by<
                        ordered_unique<
                        tag<keytag>, BOOST_MULTI_INDEX_MEMBER(MapEntry<KEYT,T>, KEYT, key) >,
                        ordered_non_unique<
                        tag<agetag>, BOOST_MULTI_INDEX_MEMBER(MapEntry<KEYT,T>, int, age) >
>
> AGEDMAPTYPE;
};
--------------------

the above was working when my MapEntry was just MapEntry<T>
but then I modified it to have two template args:
MapEntry<KEYT, T> so I could has different Key types
and have the different classes in the shared_ptr because later
I realized I would want both st::string and int to be possible keys.

But my multi_index would not compile on gcc 4.1.2 with
the MapEntry having two templates and results in error:
 
  "error: macro "BOOST_MULTI_INDEX_MEMBER" passed 4 arguments,
      but takes just 3"

so gcc thinks it's parsing out 4 args and not three.

I then thought it was the typical macro problem with parsing so I put
parens around the MapEntry first arguments to BOOST_MULTI_INDEX_MEMBER():

BOOST_MULTI_INDEX_MEMBER((MapEntry<KEYT,T>), KEYT, key) >,

This didn't help either, is it not allowed to exceed one template argument
in an argument to BOOST_MULTI_INDEX_MEMBER() so for example
MapEntry<T> is ok but MapEntry<KEYT,T> is not?

Mark


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk