POD

Andrej Mitrovic andrej.mitrovich at gmail.com
Fri Dec 28 16:23:32 PST 2012


On 12/28/12, Walter Bright <newshound2 at digitalmars.com> wrote:
> D's definition of POD:
>
> /***************************************
>   * Return true if struct is POD (Plain Old Data).
>   * This is defined as:
>   *      not nested
>   *      no postblits, constructors, destructors, or assignment operators
>   *      no fields with with any of those
>   * The idea being these are compatible with C structs.
>   *
>   * Note that D struct constructors can mean POD, since there is always
> default
>   * construction with no ctor, but that interferes with OPstrpar which wants
> it
>   * on the stack in memory, not in registers.
>   */
> bool StructDeclaration::isPOD()
> {
>      if (isnested || cpctor || postblit || ctor || dtor)
>          return false;
>
>      /* Recursively check any fields have a constructor.
>       * We should cache the results of this.
>       */
>      for (size_t i = 0; i < fields.dim; i++)
>      {
>          Dsymbol *s = fields[i];
>          VarDeclaration *v = s->isVarDeclaration();
>          assert(v && v->storage_class & STCfield);
>          if (v->storage_class & STCref)
>              continue;
>          Type *tv = v->type->toBasetype();
>          while (tv->ty == Tsarray)
>          {   TypeSArray *ta = (TypeSArray *)tv;
>              tv = tv->nextOf()->toBasetype();
>          }
>          if (tv->ty == Tstruct)
>          {   TypeStruct *ts = (TypeStruct *)tv;
>              StructDeclaration *sd = ts->sym;
>              if (!sd->isPOD())
>                  return false;
>          }
>      }
>      return true;
> }
>

I think there's a bug here, it never checks for the presence of
opAssign. Maybe change the first if check to:

 if (isnested || cpctor || postblit || ctor || dtor ||
search_function(this, Id::assign))

I can make a pull with this fix (and add the new isPOD trait). Whaddya think?


More information about the Digitalmars-d mailing list