restrict qualifier on member functions (restrict this pointer)

后端 未结 5 1521
清歌不尽
清歌不尽 2021-01-19 02:13

Note: To clarify, the question is not about the use of the restrict keyword in general, but specifically about applying it to member functions

5条回答
  •  无人共我
    2021-01-19 02:28

    The link you posted is interesting. There will not be a solid use case for having restrict applied on this. As you mentioned in your question, copy constructor, operator = could have been potential candidates; but compiler can take care of them.

    But following case can be interesting

    struct A
    {
      //...
      void Destroy (A*& p) __restrict__
      {
        delete this;
        p = 0;
        p++;
      }
    };
    

    Now use case can be;

    A **pp = new A*[10];
    for(int i = 0; i < 10; i++)
      pp[i] = new A;
    //...
    A* p = pp[0];
    for(int i = 0; i < 10; i++)
      p->Destroy(p);
    delete[] pp;
    

    Though this is very unusual practice, this is the only case I could think of this use case.

提交回复
热议问题