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

--- Comment #6 from Martin Sebor <msebor at gcc dot gnu.org> ---
As a baseline, I think it would be great to get std::string to enjoy the same
benefits as other containers WRT aliasing.  Going a step further, I'd expect
users of all containers to benefit from the aliasing guarantee the containers
already provide: that modifying one doesn't change the other.  E.g., here it
should be possible to eliminate the test:

  #include <vector>

  struct S { std::vector<int> a, b; };

  void f (struct S &s)
  {
    int t = s.a[0];
    s.b[0] = 0;
    if (t != s.a[0])   // can be folded to false
      __builtin_abort ();
  }

I've been thinking of one of two kinds of annotation that wouldn't require
programs to change and would be sufficient if applied only to the definition of
the containers: (1) one that would make "std::string::ptr" on par with that of
any other pointer other than char (i.e., a char that's not allowed to be used
to access anything but a char object), and (2) another that could basically be
described as having the effect you suggest.

An example of the former would be an attribute "noalias" applicable only to
narrow character types.  The latter could be done by applying the same
attribute (or some other) to the std::vector and std::basic_string templates.

It might take yet another extension to also express the same guarantee for
node-based containers.

Like you I'm not sure that just slapping restrict on
std::string::_M_dataplus._M_p would have the desired effect (even if GCC did
pay attention to restrict on struct members).

Reply via email to