指向成员的指针是非法引用吗?

发布于 2024-12-18 16:23:51 字数 537 浏览 2 评论 0原文

假设我有:

// This is all valid in C++11.
struct Foo {
    int i = 42;
    int& j = i;
};

// Let's take a pointer to the member "j".
auto b = &Foo::j; // Compiler is not happy here
// Note that if I tried to get a pointer to member "i", it would work, as expected.
Foo f;
std::cout << f.*b; // Try using the pointer to member

编译器抱怨我无法获取成员的地址,因为它是引用。准确地说:

语义问题:无法形成指向引用类型“int &”的成员“j”的成员指针

我知道这样做似乎毫无意义,但我只是想知道为什么不能这样做。

为什么这是不可能的?

Let us say I have:

// This is all valid in C++11.
struct Foo {
    int i = 42;
    int& j = i;
};

// Let's take a pointer to the member "j".
auto b = &Foo::j; // Compiler is not happy here
// Note that if I tried to get a pointer to member "i", it would work, as expected.
Foo f;
std::cout << f.*b; // Try using the pointer to member

The compiler complains that I cannot take the address of the member because it is a reference. To be precise:

Semantic Issue: Cannot form a pointer-to-member to member 'j' of reference type 'int &'

I know doing this seems pointless, but I am only wondering why it cannot be done.

Why is this impossible?

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

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

发布评论

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

评论(4

维持三分热 2024-12-25 16:23:51

这是无法完成的,因为您无法获取指向引用周期的指针。

如果您可以将成员指针指向引用,则这将与堆栈上引用的行为不一致。 C++的态度是引用不存在。因此,您永远无法形成指向它们的指针。

例如,&f::a 必须与 &f::b 不同。通过取消引用 &f::b,您将有效地获得指向引用的指针,这是不允许的。

It cannot be done because you cannot take a pointer to a reference- period.

If you could take a member pointer to a reference, this would be inconsistent with the behaviour of references on the stack. The attitude of C++ is that references do not exist. As such, you cannot form a pointer to them- ever.

For example, &f::a would have to be different to &f::b. And by de-referencing &f::b, you would effectively be achieving a pointer to a reference, which is not allowed.

你列表最软的妹 2024-12-25 16:23:51

C++11标准:

§8.3.3 p3 [dcl.mptr]
指向成员的指针不得指向类的静态成员 (9.4)、具有引用类型的成员或“cv void”。

另外,一般来说:

§8.3.1 p4 [dcl.ptr]
[ 注意:没有指向引用的指针;见 8.3.2。 [...]——尾注]

§8.3.2 p5 [dcl.ref]
不得有对引用的引用,不得有引用数组,并且不得有指向引用的指针

C++11 standard:

§8.3.3 p3 [dcl.mptr]
A pointer to member shall not point to a static member of a class (9.4), a member with reference type, or “cv void.”

Also, in general:

§8.3.1 p4 [dcl.ptr]
[ Note: There are no pointers to references; see 8.3.2. [...] —end note ]

§8.3.2 p5 [dcl.ref]
There shall be no references to references, no arrays of references, and no pointers to references.

对风讲故事 2024-12-25 16:23:51

成员指针(与指向成员的简单指针相反)只是结构中的偏移量,根本不是指针。您只能通过它与结构本身(或指向结构的指针)结合来获取数据:将偏移量的值添加到结构的地址,并将结果取消引用以生成成员的值。

现在假设一个成员是一个引用,因此通过它访问数据已经需要取消引用(编译器对我们隐藏它,但它需要在输出中吐出相应的指令)。如果 C++ 允许成员指针指向引用,那么它们将是另一种类型:需要添加到基址的偏移量,然后取消引用两次。改进一个已经不起眼的功能需要花费太多的工作;禁止它是一个更好的出路。

Member pointer (as opposed to a simple pointer to a member) is simply an offset into the structure, not a pointer at all. You can get data through it only in conjunction with the structure itself (or a pointer to a structure): the value of the offset is added to the address of the structure, and the result is dereferenced to produce the value of the member.

Now suppose a member is a reference, so accessing data through it already requires a dereference (compiler hides it from us, but it needs to spit out the corresponding instructions in its output). If C++ were to allow member pointers to references, they'd be of yet another type: an offset that needs to be added to the base, and then dereferenced twice. It is too much work to improve an already obscure feature; prohibiting it is a much better way out.

纸短情长 2024-12-25 16:23:51

允许您创建指向引用的指针并不会给您任何表达能力。对于这样一个庞然大物,你可以做的任何事情都不能使用引用或指针轻松完成。你从中得到的只是增加了复杂性。

为了与禁止指向引用的指针的规则保持一致,并且因为它增加了更多的复杂性,所以不允许将指针指向作为引用的成员。该语言的设计者可能认为从这些中获得的一点点收益是不值得的。

这完全只是我的意见。

Allowing you to make a pointer to a reference does not give you any expressive power. There's nothing you can do with such a beast that you can't easily do with a reference or with a pointer. All you get out of it is added complexity.

And making a pointer to a member that is a reference is not allowed for consistency with the rule that forbids pointers to references, and because it adds even more complexity. The designers of the language probably decided that the little gains you get from these was not worth it.

This is totally just my opinion.

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