为什么C ++中不存在引用到成员? [英] Why doesn't reference-to-member exist in C++?

查看:102
本文介绍了为什么C ++中不存在引用到成员?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在C ++中,我可以选择函数指针和函数引用(为了完整性,甚至可以选择函数值):

  void call_function_pointer(void(* function)()){
(* function)();
}
void call_function_reference(void(& function)()){
function();
}
void call_function_value(void function()){
function();
}

当涉及到方法时,我似乎没有这个选择

 模板< class T> void call_method_pointer(T * object,void(T :: * method)()){
(object-> * method)();
}
//以下代码创建一个编译错误
template< class T> void call_method_reference(T& object,void(T ::& method)()){
object.method();
}

这导致我假设C ++中不存在方法引用。真的吗?如果是,他们不存在的原因是什么?

解决方案

在标准中(例如 N3337 - 不是最新的,但是很好的)在8.3.3.3的结尾处有一个注释:


[注:参见5.3和5.5。类型指向成员的指针与类型指针是不同的
,也就是说,指向成员的指针仅通过成员声明符语法的指针声明为
,而不是由指针
声明符语法。在C ++中没有引用到成员类型。 -
end note]


当然,没有引用成员类型操作符,我能找到的最好的是像 - >& 。&



为什么?



至于为什么;经过一个有趣的小历史调查,没有找到任何现有的笔记(我一路回到 Cfront 2.0 其中首先支持指针成员 - 编辑:根据一个更可信的文档,该功能实际上是首先支持的在Cfront 1.2 ),我问男人自己和这里的答复:

 
日期:星期六,22二月2014 10 :12:51 -0500
来自:Bjarne Stroustrup< ...>
主题:Re:缺乏引用到成员和CFront 2.0

在2/22/2014 6:40 AM,Jason C写道:
>我的问题是:C ++非常清楚地禁止
>reference-to-member的概念。为什么是这样?我一直在做很多的
>研究,我追溯到指针到成员的回溯(I
> think)到1989年CFront 2.0。我阅读产品参考手册
>和其他文档,希望找到一些解释,但
>不能。

我真的不记得了。这是25多年前,ARM是沉默
这。我添加了指针到成员,以消除类型系统中罕见的
破坏的需要。我怀疑我没有添加对
成员的引用,因为它似乎不值得努力:没有用例。

说实话,我期待的东西更神秘,更复杂。



所以你有它:下一次有人问为什么没有引用到成员,你可以自信地说,因为没有! (注意:在评论中看到我的漫步;还有一些历史调查要做到100%的信心。)



,我从来没有在我自己的代码中使用指针到成员,但它们的存在的一个独特的理由在Stroustrup的 C ++的演变:1985-1989

,第222-223页。






顺便说一下,您调用假设参考到成员函数的语法:

  object.method(); 

...没有什么意义,因为没有办法区分语法与调用到一个名为 method()的实际成员



hvd在下面提出了一个好处:上面,在语法上,将不会有一个一致的方法来解引用引用到成员。你必须将它与正常的成员访问区分开来,但同时你想要使它与对象和函数引用(它不需要特殊的操作符)的引用一致,我真的不能想到任何实现这两者的任何东西。 / p>

In C++ I can chose between function pointers and function references (or even function values for the sake of completeness):

void call_function_pointer (void (*function)()) {
    (*function) ();
}
void call_function_reference (void (&function)()) {
    function ();
}
void call_function_value (void function()) {
    function ();
}

When it comes to methods however, I don't seem to have this choice between pointers and references.

template <class T> void call_method_pointer (T* object, void (T::*method)()) {
    (object->*method) ();
}
// the following code creates a compile error
template <class T> void call_method_reference (T& object, void (T::&method)()) {
    object.method ();
}

This leads me to the assumption that method references do not exist in C++. Is that true? If it is, what is the reason they do not exist?

解决方案

In the standard (e.g. N3337 - not the latest but fine for this) there is a note at the end of section 8.3.3.3 that reads:

[ Note: See also 5.3 and 5.5. The type "pointer to member" is distinct from the type "pointer", that is, a pointer to member is declared only by the pointer to member declarator syntax, and never by the pointer declarator syntax. There is no "reference-to-member" type in C++. — end note ]

Also, of course, there are no "reference to member" type operators (which, hypothetically, the best I can come up with would be something like ->& and .&, although these are not consistent with dereferencing of data and function references, which require no special operator).

Why?

As for why; after a fun little historical investigation and failing to find any existing notes on it (I went all the way back to Cfront 2.0 where pointer-to-member was first supported -- edit: according to a far more credible document, the feature was actually first supported in Cfront 1.2), I asked the man himself and here is the reply:

Date: Sat, 22 Feb 2014 10:12:51 -0500
From: Bjarne Stroustrup <...>
Subject: Re: On lack of reference-to-member and CFront 2.0

On 2/22/2014 6:40 AM, Jason C wrote:
> My question is: C++ very clearly disallows the concept of 
> "reference-to-member". Why is this? I have been doing a lot of 
> research, and I traced the origin of "pointer-to-member" back (I 
> think) to 1989 CFront 2.0. I read through the product reference manual 
> and other documentation hoping to find an explanation of some sort but 
> could not.

I don't really remember. It was 25+ years ago and the ARM is silent on 
this. I added pointers to members to eliminate the need for a rare 
breakage of the type system. I suspect that I didn't add references to 
members because it did not seem worth the effort: there was no use case.

To be honest, I was expecting something far more arcane and complicated.

So there you have it: The next time somebody asks why there's no reference-to-member, you can confidently say, "Because there isn't!" (Note: See my ramblings in the comments; there is still some historical investigation to be done to get to 100% confidence.)

Personally, I've never once found a use for pointers-to-members in my own code, but a distinct rationale for their existence is given in Stroustrup's The Evolution of C++: 1985-1989, pp. 222-223.


By the way, your syntax for calling the hypothetical reference-to-member function:

object.method();

... does not make much sense, as there is no way to distinguish that syntactically from a call to an actual member named method().

hvd brings up a good point below: As you can see from the above, syntactically, there wouldn't really be a consistent way to dereference a reference-to-member. You have to distinguish it from normal member access, but at the same time you want to make it consistent with dereferencing of object and function references (which require no special operator), and I can't really think of anything that accomplishes both.

这篇关于为什么C ++中不存在引用到成员?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆