dash icon indicating copy to clipboard operation
dash copied to clipboard

Container: Default initialization of elements

Open devreal opened this issue 7 years ago • 1 comments

Consider the following code snippet:

  struct point_t {
    int x, y = 1;
  };

  std::vector<point_t> vec(N);
  assert(vec[0].y == 1); // succeeds

  dash::Array<point_t> arr(N);
  assert(vec[0].y == 1); // may or may not succeed

Point to make: Contrary to the STL, elements in DASH container are not initialized with their default value. Now, there is a good argument for this: performance! Many times users might want to skip implicit initialization because they perform the initialization themselves, e.g., using dash::fill.

However, since we aim to be compatible with STL behavior and because I tapped into this trap myself (see https://github.com/dash-project/dash/pull/440/commits/b67c06572452f398eab8efe32cc4f8a3f58d9af1) I decided to raise this issue. What's more, this is mostly only relevant for derived types in DASH container because it is all too tempting to rely on the default values specified in the type definition.

As far as I can see this behavior is not documented anywhere.

Should we change the behavior to provide default initialization or make it optional (one way or another)? Or just document this deviation from the STL behavior?

devreal avatar Jan 12 '18 04:01 devreal

Hey here some input from my side: Wouldn't it be failsafer for new developer if it behaves like in STL? I suggest offering a constructor without initialization for performance aware programmers. Is this a option? Or am I fooling myself anywhere?

BenjaProg avatar Jan 12 '18 07:01 BenjaProg