Search code examples
c++stdforward-declaration

Need a C++ map and list which contain iterators to each other


I have a custom templated container using a map and list being kept in sync. The map needs to hold MyList::const_iterator and the list needs to hold MyMap::const_iterator. The only solution I've been able to find is to pun one of the iterators, as in the example below.

Is there a proper way to forward declare this so that I don't need the ugly punning?

Runnable code available at http://coliru.stacked-crooked.com/a/a5eae03ad5090b27.

(There are definitely other approaches that could be used for the example, but that's out of scope. This is a snippet of a larger program. I'm simply trying to make this "circular" definition without UB.)

#include <iostream>
#include <list>
#include <unordered_map>

template<class ObjectT> class MyClass
{
private:
// SUMMARY: The map must contain an iterator to the list, and the list must contain an iterator to the map.
// I have not been able to figure out how to define that (circular), so I've punned an iterator to a different list for the map entry.
    typedef std::list<ObjectT> PunnedList;

    struct MapEntry
    {
        ObjectT m_object;
        mutable typename PunnedList::const_iterator m_listIt; // Really a List::const_iterator, but that can't be defined.
    };

    typedef std::unordered_multimap<std::string, MapEntry> Map;

public:
    struct ListEntry
    {
        typename Map::iterator m_mapIt;

        const ObjectT& object() const
        {
            return m_mapIt->second.m_object;
        }

        const std::string& name() const
        {
            return m_mapIt->first;
        }

    };
private:
    typedef std::list<ListEntry> List;

    Map mMap;
    List mList;

private:
    typename List::const_iterator listiter_from_mapiter( typename Map::const_iterator& miter ) const
    {
        static_assert(sizeof(typename PunnedList::const_iterator) == sizeof(typename List::const_iterator));
        return *(reinterpret_cast<typename List::const_iterator*>(&miter->second.m_listIt));
    }

public:
    
    typename List::const_iterator append( const std::string &name, const ObjectT& item )
    {
        static_assert(sizeof(typename PunnedList::const_iterator) == sizeof(typename List::const_iterator));

        MapEntry entry{ item, typename PunnedList::const_iterator{} };

        auto mapIter = mMap.insert({ name, entry });
        mList.push_back({ mapIter });
        auto iter = mList.cend();
        --iter;
        *(reinterpret_cast<typename List::const_iterator*>(&mapIter->second.m_listIt)) = iter;
        return iter;
    }
    
    typename List::const_iterator begin() const
    {
        return mList.end();
    }

    typename List::const_iterator end() const
    {
        return mList.end();
    }

    void erase( typename List::const_iterator iter )
    {
        mMap.erase(iter->m_mapIt);
        mList.erase( iter );
    }

    typename List::const_iterator find( const std::string &name ) const
    {
        auto range = mMap.equal_range(name);

        for (auto mapIter = range.first; mapIter != range.second; ++mapIter)
        {
            // In the real program, there are additional criteria on the map entry, not needed for the example.
            // if (mapIter is a match)
                return listiter_from_mapiter(mapIter);
        }

         return mList.cend();
    }
};

int main()
{
    MyClass<int> container;
    container.append("A",1);
    container.append("B",2);
    container.append("C",1);
    
    std::cout << container.find("B")->object();
}

Solution

  • Forward-declaring at least one of your inner classes breaks the cycle:

    template<class ObjectT> class MyClass
    {
    public:
        struct ListEntry;
    private:
        typedef std::list<ListEntry> List;
    
        // Rest of the class, using List as you like
    

    Note that the following List::const_iterator works thanks to std::list<T> not requiring T to be complete to be instantiated.