mercredi 1 janvier 2020

Why can "Could not convert from '

With C++11, the following code snippet fails to compile due to error: could not convert '{((__Direct*)this)->__Direct::data}' from '<brace-enclosed initializer list>' to 'Direct':

struct Direct {
  uint8_t data[10][2];
};
struct __Direct {
  uint8_t data[10][2];

  Direct decompress() {
    return {data};
  }

  static __Direct compress(Direct in) {
    return {in.data};
  }
};

But I can work around this error by wrapping the multi-dimensional C array in a struct:

struct Wrapper {
  uint8_t data[10][2];
};
struct Wrapped {
  Wrapper tbl;
};
struct __Wrapped {
  Wrapper tbl;

  Wrapped decompress() {
    return {tbl};
  }

  static __Wrapped compress(Wrapped in) {
    return {in.tbl};
  }
};

Logically it seems like these could compile the same. I suspect C++ does not do bound checking when it encounters the explicit passing of a multi-dimensional C array (and is attempting to cast as uint8_t*[], causing the brace initializer to not match).

What C++ specification(s) describe this behavior?

Aucun commentaire:

Enregistrer un commentaire