下标运算符后缀
C++ 标准将使用下标的表达式定义为后缀表达式。 AFAIK,这个运算符总是带有两个参数(第一个是指向 T 的指针,另一个是枚举或整数类型)。因此它应该符合二元运算符的资格。
那么问题来了,什么是下标运算符?它是一元还是二进制?当然,它不是一元的,因为它在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我倾向于同意你的观点,
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]
, wherex
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 thatoperator[]
must be a nonstatic member function.您是正确的,
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我认为重要的是 [] 所使用的上下文。第 5.2.1 节符号 [] 用于“与 *((E1)+(E2)) 相同(根据定义)”的后缀表达式的上下文中。在这种情况下,[] 不是运算符。在第 13.5.5 节中,它用于表示下标运算符。在本例中,它是一个带有一个参数的运算符。例如,如果我写:
上述语句的计算结果不一定是:
因为 '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:
It's not necessarily the case that the above statement evaluates to:
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.
如果您仔细查看 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.