与外部“C”的友谊函数似乎需要 :: 来限定名称

发布于 2024-12-10 11:12:26 字数 737 浏览 0 评论 0原文

尝试使用 extern "C" 函数创建一个 class 朋友,这段代码有效:

#include <iostream>

extern "C" {
  void foo();
}

namespace {
  struct bar {
    // without :: this refuses to compile
    friend void ::foo();
    bar() : v(666) {}
  private:
    int v;
  } inst;
}

int main() {
  foo();
}

extern "C" {
  void foo() {
    std::cout << inst.v << std::endl;
  }
}

但我非常惊讶地发现,使用 g++ 4.6.1 和 4.4.4 我必须在 friend void ::foo(); 中明确写入 ::,否则友谊不起作用。这个 :: 仅当它是 extern "C" 时才需要。

  1. 这是编译器错误/问题吗?我没想到会有这种行为。
  2. 如果这不是一个错误,为什么需要这样做,但只有当它是 extern "C" 时而不是没有它时?名称查找规则的变化使得这成为必要吗?

我很困惑。可能有一些我找不到的规则。

Trying to make a class friends with an extern "C" function, this code works:

#include <iostream>

extern "C" {
  void foo();
}

namespace {
  struct bar {
    // without :: this refuses to compile
    friend void ::foo();
    bar() : v(666) {}
  private:
    int v;
  } inst;
}

int main() {
  foo();
}

extern "C" {
  void foo() {
    std::cout << inst.v << std::endl;
  }
}

But I was very surprised to find that with g++ 4.6.1 and 4.4.4 I have to explicitly write :: in friend void ::foo(); otherwise the friendship doesn't work. This :: is only needed when it's extern "C" though.

  1. Is this a compiler bug/problem? I wasn't expecting that behaviour.
  2. If it isn't a bug why is this required, but only when it's extern "C" and not without it? What about the name lookup rules changes that makes this necessary?

I'm stumped. There is probably some rule for this that I can't find.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

黯淡〆 2024-12-17 11:12:26

[n3290: 7.3.1.2/3]: 命名空间中首先声明的每个名称都是一个
该命名空间的成员。如果非本地类中的 friend 声明
首先声明一个类或函数,友元类或函数是
最内层封闭命名空间的成员。朋友的名字是
通过非限定查找 (3.4.1) 或限定查找 (3.4.3) 未找到
95) 这意味着类或函数的名称是
不合格。直到其中提供了匹配的声明
命名空间范围(在类定义授予之前或之后
友谊)。如果调用友元函数,则可以通过以下方式找到其名称
考虑命名空间和类中的函数的名称查找
与函数参数的类型相关联(3.4.2)。 如果
friend 声明中的名称既不是限定的也不是 template-id
并且声明是一个函数或一个详细类型说明符
查找以确定该实体之前是否已声明
不应考虑最内层外围之外的任何范围
命名空间。
[..]

最内层的封闭命名空间是匿名命名空间,并且您没有限定函数名称,因此 该名称不是发现

命名空间也不必是匿名的

请注意,问题中的extern“C”是一个转移注意力的问题,因为以下内容也因同样的原因而失败:

void foo();

namespace {
struct T {
   friend void foo();

   private: void bar() { cout << "!"; }
} t;
}

void foo() { t.bar(); }

int main() {
   foo();
}

/*
In function 'void foo()':
Line 7: error: 'void<unnamed>::T::bar()' is private
compilation terminated due to -Wfatal-errors.
*/

[替代测试用例,改编自您的原始代码]

[n3290: 7.3.1.2/3]: Every name first declared in a namespace is a
member of that namespace. If a friend declaration in a non-local class
first declares a class or function the friend class or function is a
member of the innermost enclosing namespace. The name of the friend is
not found by unqualified lookup (3.4.1) or by qualified lookup (3.4.3)
95) this implies that the name of the class or function is
unqualified. until a matching declaration is provided in that
namespace scope (either before or after the class definition granting
friendship). If a friend function is called, its name may be found by
the name lookup that considers functions from namespaces and classes
associated with the types of the function arguments (3.4.2). If the
name in a friend declaration is neither qualified nor a template-id
and the declaration is a function or an elaborated-type-specifier, the
lookup to determine whether the entity has been previously declared
shall not consider any scopes outside the innermost enclosing
namespace.
[..]

The innermost enclosing namespace is the anonymous one, and you didn't qualify the function name, so the name is not found.

The namespace need not be anonymous, either.

Note that the extern "C" in the question is a red herring, as the following also fails for the same reason:

void foo();

namespace {
struct T {
   friend void foo();

   private: void bar() { cout << "!"; }
} t;
}

void foo() { t.bar(); }

int main() {
   foo();
}

/*
In function 'void foo()':
Line 7: error: 'void<unnamed>::T::bar()' is private
compilation terminated due to -Wfatal-errors.
*/

[alternative testcase, adapted from your original code]

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文