在C++中关于此参数的矛盾标准

发布于 2025-01-22 04:08:25 字数 1290 浏览 0 评论 0原文

我正在学习C ++的课程。特别是,我知道是非静态成员函数中的 prvalue 表达式。

来自 class.this#1

在非静态成员函数的正文中,关键字是 prvalue表达式,其值是该函数称为...

的对象的地址

但是在同一文档中,我遇到了 expr.call.call#4

调用函数时,每个参数([dcl.fct])应初始化([dcl.init],[class.copy],[class.ctor])及其相应的参数。 如果该函数是非静态成员函数,则应用指向呼叫对象的指针初始化该函数的参数,就像通过显式转换类型转换...


现在我们知道,如果我们具有名为var的函数参数,那么该参数的值类别var(作为表达式)为 lvalue 。因此,读取上面的第二个引用语句(这意味着非静态成员函数具有this参数),应适用于this参数。特别是,如果确实是非静态成员函数的参数,则其值类别(作为表达式)应为 lvalue

这使我得出这样一个结论,即标准在上述2个引用陈述的上下文中与自身矛盾,因为第一个引用的语句说是A prvalue表达式,而第二引号语句暗示 lvalue表达式

我的问题是,为什么标准委员会没有从标准中删除这种矛盾,因为它自然而然地从上面的两个引用陈述中遵循。另外,是否有一种方法可以安全地消除这一矛盾。

PS:请注意,在上面的讨论中,我说“第二引用的陈述意味着非静态成员函数具有参数” ”,因为如果没有代码>此参数,然后语句“如果该函数是非静态成员函数,则应初始化该函数的参数...” 由标准给出没有意义。

I am learning about classes in C++. In particular, i came to know that this is a prvalue expression inside a non-static member function.

From class.this#1:

In the body of a non-static member function, the keyword this is a prvalue expression whose value is the address of the object for which the function is called...

But in the same document i came across expr.call#4:

When a function is called, each parameter ([dcl.fct]) shall be initialized ([dcl.init], [class.copy], [class.ctor]) with its corresponding argument. If the function is a non-static member function, the this parameter of the function shall be initialized with a pointer to the object of the call, converted as if by an explicit type conversion...


Now we know that if we have a function parameter named var, then the value category of that parameter var(as an expression) is an lvalue. Thus, reading the 2nd quoted statement above(that implies that a non-static member function has a this parameter), the same should apply to the this parameter. In particular, if this is indeed a parameter of a non-static member function then its value category(as an expression) should be lvalue.

This leads me to the conclusion that the standard contradicts itself in the context of the above 2 quoted statements because the 1st quoted statement says this is a prvalue expression while the 2nd quoted statement implies that this is an lvalue expression.

My question is that why this contradiction isn't removed from the standard by the standard committee since it follows quite naturally from the above two quoted statement. Also, is there a way to safely remove this contradiction.

PS: Note that in the above discussion, i said that "the 2nd quoted statement implies that a non-static member function has a this parameter" because if there is no this parameter then the statement "If the function is a non-static member function, the this parameter of the function shall be initialized..." given by the standard wouldn't make sense.

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文