Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added static asserting vector components' member ordering and padding #285

Merged
merged 1 commit into from
Mar 12, 2024

Conversation

m-fila
Copy link
Contributor

@m-fila m-fila commented Mar 12, 2024

BEGINRELEASENOTES

  • added static asserting vector components' member ordering and padding

ENDRELEASENOTES

The operator[] of generated vector classes assumes no padding and member ordering same as in the source code. These are reasonable assumptions but not required by the standard. The assumptions can be checked at a compile time

Copy link
Contributor

@tmadlener tmadlener left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suppose we really need to check all members if we want to be sure and we don't have a shortcut of assuming that, e.g. z should be OK, if x and y have no padding?

Apart from the fact that our yaml files become a bit noisy, I really like this solution, and from my side I think this can be merged.

@tmadlener
Copy link
Contributor

release based CI is failing because AIDASoft/podio#560 is not yet in the release.

@jmcarcell
Copy link
Contributor

Only one nitpick:

The operator[] of generated vector classes assumes no padding and member ordering same as in the source code. These are reasonable assumptions but not required by the standard. The assumptions can be checked at a compile time

In this case the ordering is guaranteed; what we are missing to have no padding is having a default constructor. See here https://learn.microsoft.com/en-us/cpp/cpp/trivial-standard-layout-and-pod-types?view=msvc-170, although I don't know if what I say about the ordering is here but this can be found elsewhere

@jmcarcell jmcarcell merged commit 69a9738 into key4hep:main Mar 12, 2024
9 of 13 checks passed
@tmadlener
Copy link
Contributor

I am not sure we need a default constructor as we generate the following:

struct Vector3f {
  float x{};
  float y{};
  float z{};
}

So at least from an initialization point of view the members are default initialized.

@jmcarcell
Copy link
Contributor

jmcarcell commented Mar 12, 2024

For not having padding you need it and not to initialize the members

struct Vector3f {
  float x{};
  float y{};
  float z{};

  Vector3f() = default;

};

struct Vector3f2 {
  float x;
  float y;
  float z;

  Vector3f2() = default;

};

int main()
{
  cout << is_pod<Vector3f>::value << endl;  // not POD
  cout << is_pod<Vector3f2>::value << endl; // POD
  return 0;
}

so we would also have to remove the initialization of the members

@tmadlener
Copy link
Contributor

I think padding and initialization are two different things here(?). std::is_pod is effectively std::is_trivial && std::is_standard_layout (IIUC). What we test for in podio is std::is_standard_layout as that is what we are mainly interested in. (Corresponding unit test for the Data types)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants