下标运算符后缀

发布于 2024-10-04 12:43:09 字数 477 浏览 3 评论 0原文

C++ 标准将使用下标的表达式定义为后缀表达式。 AFAIK,这个运算符总是带有两个参数(第一个是指向 T 的指针,另一个是枚举或整数类型)。因此它应该符合二元运算符的资格。

但是 MSDNIBM 未将其列为二元运算符。

那么问题来了,什么是下标运算符?它是一元还是二进制?当然,它不是一元的,因为它在 5.3 美元中没有提到(至少是直截了当的)。

当标准提到它在后缀表达式上下文中的用法时,这意味着什么?

The C++ standard defines the expression using subscripts as a postfix expression. AFAIK, this operator always takes two arguments (the first is the pointer to T and the other is the enum or integral type). Hence it should qualify as a binary operator.

However MSDN and IBM does not list it as a binary operator.

So the question is, what is subscript operator? Is it unary or binary? For sure, it is not unary as it is not mentioned in $5.3 (at least straigt away).

What does it mean when the Standard mentions it's usage in the context of postfix expression?

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

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

发布评论

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

评论(4

温柔戏命师 2024-10-11 12:43:09

我倾向于同意你的观点,operator[] 是最严格意义上的二元运算符,因为它确实需要两个参数:对对象的(可能是隐式的)引用和值其他一些类型(不一定是枚举或整数)。但是,由于它是一个括号运算符,您可能会说标记序列 [x](其中 x 可以是任何有效的下标表达式)符合后缀一元的条件抽象意义上的运算符;想想柯里化。

此外,例如,您不能重载全局 operator[](const C&, size_t)。编译器抱怨 operator[] 必须是非静态成员函数。

I'd tend to agree with you in that operator[] is a binary operator in the strictest sense, since it does take two arguments: a (possibly implicit) reference to an object, and a value of some other type (not necessarily enumerated or integral). However, since it is a bracketing operator, you might say that the sequence of tokens [x], where x might be any valid subscript-expression, qualifies as a postfix unary operator in an abstract sense; think currying.

Also, you cannot overload a global operator[](const C&, size_t), for example. The compiler complains that operator[] must be a nonstatic member function.

梦罢 2024-10-11 12:43:09

您是正确的,operator[] 是一个二元运算符,但它的特殊之处在于它也必须是一个成员函数。

operator() 类似,

您可以阅读后缀表达式这里

我刚刚发现了一篇关于operator[]和后缀表达式的有趣文章,此处

You are correct that operator[] is a binary operator but it is special in that it must also be a member function.

Similar to operator()

You can read up on postfix expressions here

I just found an interesting article about operator[] and postfix expression, here

满地尘埃落定 2024-10-11 12:43:09

我认为重要的是 [] 所使用的上下文。第 5.2.1 节符号 [] 用于“与 *((E1)+(E2)) 相同(根据定义)”的后缀表达式的上下文中。在这种情况下,[] 不是运算符。在第 13.5.5 节中,它用于表示下标运算符。在本例中,它是一个带有一个参数的运算符。例如,如果我写:

 x = a[2];

上述语句的计算结果不一定是:

x = *(a + 2);

因为 'a' 可能是一个对象。如果 a 是对象类型,则在此上下文中,[] 用作下标运算符。

无论如何,这是我可以从解决明显矛盾的标准中得出的最佳解释。

I think it's the context that [] is used in that counts. Section 5.2.1 the symbol [] is used in the context of a postfix expression that is 'is identical (by definition) to *((E1)+(E2))'. In this context, [] isn't an operator. In section 13.5.5 its used to mean the subscripting operator. In this case it's an operator that takes one argument. For example, if I wrote:

 x = a[2];

It's not necessarily the case that the above statement evaluates to:

x = *(a + 2);

because 'a' might be an object. If a is an object type then in this context, [] is used as an subscript operator.

Anyway that's the best explanation I can derive from the standard that resolves apparent contradictions.

滥情空心 2024-10-11 12:43:09

如果您仔细查看 http://en.wikipedia.org/wiki/Operators_in_C_and_C% 2B%2B它将向您解释标准C++将operator[]识别为二元运算符,正如您所说。
一般来说,Operator[] 是二进制的,并且尽管有可能将其变为一元,但它应该始终在类内部用作二进制,即使它在类外部没有任何意义。

我为您提供的链接中对此进行了很好的解释...
请注意,有时许多程序员在没有过多考虑它们在做什么的情况下重载运算符,有时以不正确的方式重载它们;编译器很容易接受它并接受它,但是,这可能不是重载该运算符的正确方法。

遵循我为您提供的指南,是以正确方式做事的好方法。

因此,请始终注意操作符在没有良好实践(不符合标准)的情况下重载的示例,首先参考标准方法,然后使用符合标准方法的示例。

If you take a close look to http://en.wikipedia.org/wiki/Operators_in_C_and_C%2B%2B it will explain you that standard C++ recognize operator[] to be a binary operator, as you said.
Operator[] is, generally speaking, binary, and, despite there is the possibility to make it unary, it should always be used as binary inside a class, even because it has no sense outside a class.

It is well explained in the link I provided you...
Notice that sometimes many programmers overload operators without think too much about what they are doing, sometimes overloading them in an incorrect manner; the compiler is ease is this and accept it, but, probably, it was not the correct way to overload that operator.

Following guides like the one I provided you, is a good way to do things in the correct manner.

So, always beware examples where operators are overloaded without a good practice (out of standard), refer, first to the standard methods, and use those examples that are compliant to them.

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