Are clang++ and libc++ compatible?

David Chisnall theraven at FreeBSD.org
Wed Nov 13 10:04:44 UTC 2013


On 12 Nov 2013, at 18:21, John Baldwin <jhb at freebsd.org> wrote:

>> struct foo {
>> struct foo bar;
>> }
> 
> Except it isn't.  It's declaring the head of a container.  This is more
> like:
> 
> 	struct foo {
> 		TAILQ_HEAD(, foo) messages;
> 	};

Eitan is correct here.  The definition of std::deque is that it copies the value that is the template argument and does not require modifications to the layout.  A deque is more akin to an array, so in C it would be something like:

struct foo {
  struct foo bar[10];
};

This is clearly nonsense - you can't have a structure that contains itself.  The same is true for the deque.  It's not clear what the pan people actually wanted, but an efficient implementation of a deque would most likely contain space for a small number of the template argument elements, so they are literally defining a structure containing a structure containing the parent structure.  The same would be true if they did:

struct Entry {
    std::vector<Entry> v;
};

An implementation of the vector class might allocate all of the elements on the heap lazily, but it's not required to and could equally have space for a small number inside the object, expanding to something like this:

struct Entry {
   struct MangledNameOfVectorOfEntry {
      size_t size;
      Entry small[4];
      Entry *ptr;
   };
};

It would make sense to have a std:deque<Entry&> or std:deque<Entry*>, because then you're only storing references or pointers to the outer structure in the inner structure.  

David



More information about the freebsd-current mailing list