restrict qualifier on member functions (restrict this pointer)

后端 未结 5 1517
清歌不尽
清歌不尽 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:29

    Either I am missing something, or your question does not make sense. this is not that different from any other argument to a member function, so why are you surprised that GCC allows you to apply restrict to it?

    Regarding applying it to an assignment operator, you rightly point out that it would obviate the need for an explicit self-assignment test. Then you say:

    Obviously, this is something that you cannot possibly know in advance

    But this is always true when you use restrict for anything. For example, somebody might decide to call memcpy with overlapping memory regions; you "cannot possibly know in advance" that they will not do so. But the restrict declaration for the arguments of memcpy means they have committed an error if they do. In exactly the same way, if you declare an assignment operator restrict, you have made it an error for someone to self-assign objects of that class. There is nothing mysterious or contradictory about this at all; it is just part of the semantics of restrict that it imposes certain constraints on the rest of your code.

    I am also not sure why you find it so impossible for a member function to take a pointer (or reference) to another object of the same type. Trivial example:

    class Point {
    public:
        double distance(const Point &other) const;
    };
    

    This sort of thing crops up all the time.

    So the real question is, why do you think this is so different from any other argument? Or if you prefer, how did I miss your point so completely?

提交回复
热议问题