是 。和->在 C 和 C++真的是运营商吗?

发布于 2024-10-04 23:24:23 字数 216 浏览 7 评论 0原文

你可能一直很坚强,你自己也很坚强。和->是检索结构 (C) 或类 (C++) 成员的运算符

但是,我怀疑它们是运算符 - 因为,如果它们是运算符,那么它们的输入类型是什么?此外,两侧的标识符是相互依赖的 - 例如 + 运算符所缺乏的功能。

如果这是正确的 - 在实践中它们在什么意义上仍然被标记为运算符,以及它们在语言理论方面的正式定义是什么。

you probably have been tought, are tought yourselves, that . and -> are operators which retrieve members of a struct (C) or class (C++).

However, I doubt they are operators - because, if they are operators, what are their input types? Furthermore, the identifiers on both sides are interdependent - a feature which for example the + operator lacks of.

If this is correct - in what sense are these still labeled as operator in practice, and what is their formal definition with regard to language theory.

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

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

发布评论

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

评论(8

相守太难 2024-10-11 23:24:23

您假设唯一可以作为参数传递给运算符的类型是可以在语言中定义的类型。

我认为编译器可以识别的任何类型都可以作为参数传递,包括内部类型,例如“标识符”。该运算符在其 AST 表示中将有两个参数,这足以让您定义语义。

另一个论点是,语言理论可能为你的词汇提供一组定义,但它不是唯一的。

例如,操作员可能是操作机器的人。该定义与编程理论无关,但它不会阻止我在特定领域语言中使用关键字来表达与机器操作有关的内容。同样,术语“运算符”在数学中的定义比特定于编程理论的定义更广泛 - 并且该定义不会仅仅因为使用编程语言而失效。

换句话说,如果您不称其为运算符,您会称其为什么?

编辑

为了澄清,我的第一个参数是指使用运算符(调用)的语法。这些运算符具有正确的参数,它们是标识符(成员名称),C++ 语言无法使用数据类型来表达它们。 C++ 语言确实有成员指针,但它们与成员不同——就像变量与指向该变量的指针不同一样。

我想这就是问题所指的内容。这些运算符的正确参数具有无法在语言中正常表达或操作的类型。

当该语法映射到重载的 operator-> 函数时会发生什么情况是另一回事。函数不是运算符——它只是运算符的实现方式。

You assume that the only types which can be passed as arguments to an operator are types that can be defined within the language.

I would argue that any type which can be recognised by the compiler may be passed as an argument, including internal types such as "identifier". The operator will have two arguments in its AST representation, which is enough to allow you to define semantics.

Another argument is that language theory may provide one set of definitions for your vocabulary, but it isn't the only one.

For example, an operator may be a man who works a machine. That definition has no relevance to programming theory, but it won't stop me using for keywords in a domain-specific language expressing something to do with machine operating. Similarly, the term "operator" has a wider definition in mathematics than that which is specific to programming theory - and that definition isn't invalidated simply by working with a programming language.

To put it another way - if you didn't call it an operator, what would you call it?

EDIT

To clarify, my first argument is referring to the syntax for using the operator (the call). These operators have right arguments which are identifiers - member names - which the C++ language cannot express using a data type. The C++ language does have member pointers, but they aren't the same thing as the members - just as a variable isn't the same as a pointer to that variable.

I assume that is what the question referred to. The right parameter of those operators has a type which cannot be expressed or manipulated normally in the language.

What happens when that syntax is mapped to an overloaded operator-> function is a different thing. The function isn't the operator - it's only how the operator gets implemented.

罪歌 2024-10-11 23:24:23

我认为您可以使用“operator”关键字重载 -> 运算符这一事实应该是一个致命的赠品。

智能指针经常这样做:

template<class T>
struct myPtr {
    T *operator ->()    { return m_ptr; }

    private:
        T *m_ptr;
};

. 不可重载,但根据定义也是一个运算符。

I think the fact that you can overload the -> operator using the "operator" keyword should be a dead giveaway.

Smart pointers do it pretty often:

template<class T>
struct myPtr {
    T *operator ->()    { return m_ptr; }

    private:
        T *m_ptr;
};

The . is not overloadable, but is also an operator by definition.

罗罗贝儿 2024-10-11 23:24:23

嗯...sizeof 是一个运算符,它的输入类型是什么?我认为这个问题对于在这种情况下区分操作员和非操作员没有用。

是因为“运算符”在编程语言的上下文中的含义完全就是该语言的作者所说的它的含义。这里有刘易斯·卡罗尔的影子。

Hmmm...sizeof is an operator, what is its input type? I don't think the question is useful for distinguishing operators from non-operators in this context.

And that would be because what "operator" means in the context of a programming language is exactly what the author of the language says it means. Shades of Lewis Carroll here.

◇流星雨 2024-10-11 23:24:23

此参考文献说它们都是 C++ 中的运算符:

http://www.cplusplus.com/doc /tutorial/operators/

这还不够权威吗?

This reference says they're both operators in C++:

http://www.cplusplus.com/doc/tutorial/operators/

Is that not authoritative enough?

猛虎独行 2024-10-11 23:24:23

你可以重载 ->运算符:维基百科。该页面还指出您不能超载 dot.有一个例子 ->重载此处

class String // this is handle
{
  ...
  Stringrep *operator -> () const { return b_; }
  private:
  Stringrep *b_;
}

箭头作用于箭头左侧的值并返回左侧的任何值侧面是“握在里面”。想象一下智能指针

You can overload the -> operator: Wikipedia. That page also states that you can't overload dot. There's an example of -> overloading here:

class String // this is handle
{
  ...
  Stringrep *operator -> () const { return b_; }
  private:
  Stringrep *b_;
}

The arrow works on the value to the left of the arrow and returns whatever the left hand side is "holding inside". Think of a smart pointer.

耀眼的星火 2024-10-11 23:24:23

C++03 标准将两者都称为运算符。

例子:

...之后。应用于其类的类型的表达式的运算符...

如果您不熟悉该术语,您可以使用术语标点符号来表示 .

THe C++03 standard refers to both as operators.

Example:

...after the . operator applied to an expression of the type of its class...

If you are not comfortable with that terminology you can use the term punctuator for ..

江城子 2024-10-11 23:24:23

在线 C 标准 (n1256):

6.5.2.3 结构和联合成员

约束

1 . 运算符的第一个操作数应具有限定或非限定的结构或联合
类型,第二个操作数应命名该类型的成员。

2 -> 运算符的第一个操作数的类型应为“指向限定或非限定的指针”
结构体”或“指向合格或不合格联合体的指针”,第二个操作数应
命名所指向类型的成员。

它们是运算符,其输入类型由标准指定。

Online C standard (n1256):

6.5.2.3 Structure and union members

Constraints

1 The first operand of the . operator shall have a qualified or unqualified structure or union
type, and the second operand shall name a member of that type.

2 The first operand of the -> operator shall have type ‘‘pointer to qualified or unqualified
structure’’ or ‘‘pointer to qualified or unqualified union’’, and the second operand shall
name a member of the type pointed to.

They are operators, and their input types are specified by the standard.

溇涏 2024-10-11 23:24:23

哈哈,我知道人们已经以迂回的方式说过这句话,但只是直接说出来。在C语言中,label->实际上是 (*label) 的简写。话虽这么说,。是引用结构中元素的运算符。因此,->引用指向结构的指针中的元素。

haha, i know people have already said this in a roundabout way but just to say it directly. In C terms, label-> is actually a shorthand for (*label). .That being said, . is the operator which references elements in a struct. Therefore, -> references an element in a pointer to a struct.

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