slot_map icon indicating copy to clipboard operation
slot_map copied to clipboard

Recursive struct with slotmap keys

Open soudy opened this issue 10 months ago • 1 comments

Hi there! Thanks for the work on this library, it's a very useful tool.

I am trying to implement a slotmap of structs whose fields can hold slotmap keys to its own type. That is, I'm trying to do something like this:

struct HalfEdge;

using HalfEdgeSM = dod::slot_map32<HalfEdge>;

struct HalfEdge {
  using key = HalfEdgeSM::key;

  std::optional<key> twin;
  std::optional<key> next;
};

This, of course, does not work as the size of HalfEdge isn't known:

In file included from /home/soud/SLCU/growth-halfedge/src/main.cc:6:
/home/soud/SLCU/growth-halfedge/include/thirdparty/slot_map.h: In instantiation of ‘class dod::slot_map<HalfEdge, dod::slot_map_key32<HalfEdge>, 4096, 64>’:
/home/soud/SLCU/growth-halfedge/src/main.cc:60:27:   required from here
/home/soud/SLCU/growth-halfedge/include/thirdparty/slot_map.h:401:56: error: invalid application of ‘sizeof’ to incomplete type ‘HalfEdge’
  401 |     using ValueStorage = typename std::aligned_storage<sizeof(T), alignof(T)>::type;
      |                                                        ^~~~~~~~~

I currently worked around this by storing uint32_t and recasting them to HalfEdgeSM::key for get calls and such:

struct HalfEdge {
  using key = uint32_t;

  std::optional<key> twin;
  std::optional<key> next;
};

// in code...
HalfEdgeSM halfedges;
HalfEdgeSM::key next_he_corr{*he->twin};  // create key from raw key
auto next_corr = halfedges.get(next_he_corr);

I was wondering if you could think of a better way to implement something like this? Or if there is any way to support this?

soudy avatar Feb 27 '25 16:02 soudy