Name which introduced by friend declaration

南楼画角 提交于 2019-12-05 18:39:02

It can be found by Argument Dependent Lookup, but only that way.

E.g. you can implement a comparison operator that way:

struct Point
{
    int x, y;

    friend
    auto operator<( Point const a, Point const b )
        -> bool
    {
        // compare and return true or false
    }
};

The name (here operator<) is not visible in the enclosing scope, but is found when the function is invoked with arguments of type Point.


The current rules are designed to be backward-compatible with the so called Barton-Nackman trick.

Quoting the Wikpedia article on that:

“When investigating the possibility of removing friend name injection from the C++ programming language, Barton and Nackman’s idiom was found to be the only reasonable use of that language rule. Eventually, the rules for argument-dependent lookup were adjusted to replace friend name injection by a less drastic mechanism, described above, that maintained the validity of Barton and Nackman’s technique”

where “friend name injection” refers to earlier rules where the name did become visible in the enclosing scope.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!