- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我有一个在类头中定义了友元函数的类。当我对该友元函数进行 using 声明时,它在 MSVC2017 中有效,但在 GCC 8.2 或 Clang 7.0 中均无效。谁是对的?这是 Godbolt 链接 ( https://godbolt.org/z/_7MVlh ),这是代码:
namespace vec {
class Vec {
friend Vec vec_max(const Vec& a, const Vec& b) { return a; }
};
}
void test() {
using vec::vec_max;
}
GCC 在使用声明时给出错误:“错误:命名空间‘vec’中没有名为‘vec_max’的成员”。 Clang 给出了类似的信息。 MSVC 按预期编译它。
根据我的理解,vec_max 应该驻留在 vec 命名空间中,而 MSVC 应该是正确的。但是标准中可能有一些微妙的文字,使得对 gcc 和 clang 的更严格的解释是正确的(尽管不太直观)。
最佳答案
MSVC 在这里是错误的。如果友元函数是在类定义中内联定义的,而不是在类定义外部声明的,则不应通过正常的名称查找找到它,只有 Argument-Dependent Lookup 应该能够找到它。
引用 C++17 (n4659):
14.3 [类(class) friend ]
6 A function can be defined in a friend declaration of a class if and only if the class is a non-local class (12.4), the function name is unqualified, and the function has namespace scope. ...
7 Such a function is implicitly an inline function (10.1.6). A
friend
function defined in a class is in the (lexical) scope of the class in which it is defined. A friend function defined outside the class is not (6.4.1).
和 6.4.2/4 [basic.lookup.argdep]
When considering an associated namespace, the lookup is the same as the lookup performed when the associated namespace is used as a qualifier (6.4.3.2) except that:
(4.1) ...
(4.2) Any namespace-scope friend functions or friend function templates declared in associated classes are visible within their respective namespaces even if they are not visible during an ordinary lookup (14.3).
- (4.3) ...
关于c++ - 如果 using 声明能够引用友元函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53409706/
我正在使用 Java 编写一个时钟程序,该程序能够“滴答作响”,但它存在问题。我认为它与 getter 和 setter 或 toString() 方法有关。 计数器类 package clock;
const Index = () => { // Ref Links const frefLinks = { 1: useRef(1), 2: useRef(2), 3: useRef(3
所以我读了here不能 pickle 装饰函数。确实: import multiprocessing as mp def deco(f): def wrapper(*args, **kwarg
我在go1.11.2 linux/amd64 版本。当包godog使用 go get github.com/DATA-DOG/godog/ 安装,godog 可执行文件在 $GOPATH/bin/中创
如何正确压缩字符串,以便 PHP 能够解压缩? 我试过这个: public static byte[] compress(String string) throws IOException {
我们这里的问题是表明 在测试中使用 Kleene 代数。 在 b 的值由 p 保留的情况下,我们有交换条件 bp = pb;两个程序之间的等价性简化为等式 在 b 的值不被 p 保留的情况下,我们有交
我有一个与我的网络相关的非常奇怪的问题,我在具有多个接口(interface)的 VirtualBox 上安装了 RDO Grizzly OpenStack。 虚拟盒子: eth0 - managem
我正在尝试使用 Passport.js授权谷歌OAuth2在 Node.js .我整个星期都在尝试让它工作,但不知道为什么它不工作,所以现在我求助于 stack 寻求一些潜在的帮助。我已经尝试了所有在
我是一名优秀的程序员,十分优秀!