Runtime error with default/copy constructor


The code below does not construct a vec1 object with the expected value. I believe the sole element of the std::array base component of d_v3 should be initialised to 3; and of d_v0, to 0. The friend function should then add these to provide a vec1 rvalue to the d_vv constructor:

#include <cstdio>
#include <array>
#include <cassert>

struct vec1 : public std::array<std::size_t, 1>
  vec1() : std::array<std::size_t, 1>{} { }

  vec1(std::size_t x) : std::array<std::size_t,1>{x} { }

  friend vec1 operator+(const vec1& a, const vec1& b) { return {a[0] + b[0]}; }

void doit()
  const vec1 d_v3{3};
  const vec1 d_v0 = {}; // now zero
  vec1 d_vv{d_v3+d_v0};

  printf("%ld\n", d_vv[0]);

int main(int argc, char *argv[])
  const vec1 v3{3};
  const vec1 v0 = {}; // now zero
  vec1 vv{v3+v0};



  return 0;

I am working on Ubuntu 21.04 with nvc++ 22.2. The invocation is nvc++ -stdpar default-ctor.cpp. The assert fires, but I think it shouldn’t. I suspect the implicit copy and move constructors are involved.


Thanks Paul. I’ve added a problem report, TPR #31413, and sent it to engineering for further investigation.

Note that I can work around the issue if I explicitly initialize d_v0, i.e. use “const vec1 d_v0{0};”.


1 Like