https://gcc.gnu.org/bugzilla/show_bug.cgi?id=103295

--- Comment #7 from Jonathan Wakely <redi at gcc dot gnu.org> ---
The code can be reduced to this, which GCC, EDG and MSVC all accept, but Clang
doesn't:

struct S
{
  union {
    char buf[8];
    char* ptr;
  };
  unsigned len;

  constexpr S(const char* s, unsigned n)
  {
    char* p;
    if (n > 7)
      p = ptr = new char[n];
    else
      p = buf;
    for (len = 0; len < n; ++len)
      p[len] = s[len];
    p[len] = '\0';
  }

  constexpr ~S()
  {
    if (len > 7)
      delete[] ptr;
  }
};

constexpr bool test()
{
  S s("test", 4);
  return true;
}

static_assert( test() );


Clang accepts it if written like this:

  constexpr S(const char* s, unsigned n)
  {
    if (n > max_size())
    {
      ptr = new char[n];
      for (len = 0; len < n; ++len)
        ptr[len] = s[len];
      ptr[len] = '\0';
    }
    else
    {
      for (len = 0; len < n; ++len)
        buf[len] = s[len];
      buf[len] = '\0';
    }
  }

It just doesn't allow assignment through the pointer.

Reply via email to