运算符与函数的行为

huangapple go评论98阅读模式
英文:

Operator vs functions behaviour

问题

我正在阅读以下文件,

https://code.google.com/p/go-wiki/wiki/GoForCPPProgrammers

并发现下面的陈述有点模糊:

> 与C++不同,new是一个函数,而不是一个运算符;new int是一个语法错误。

在C++中,我们将运算符实现为函数,例如使用operator+实现+

那么在编程语言中,_运算符_与_函数_的确切区别是什么?

英文:

I am reading through the following document,

https://code.google.com/p/go-wiki/wiki/GoForCPPProgrammers

and found the statement below a bit ambiguous:

> Unlike in C++, new is a function, not an operator; new int is a syntax error.

In C++ we implement operators as functions, e.g. + using operator+.

So what is the exact difference of operator vs function in programming languages in general?

答案1

得分: 2

实际上,函数和运算符之间的区别取决于编程语言。在纯C中,运算符是语言本身的一部分。不能添加运算符,也不能更改现有运算符的行为。但在C++中不同,运算符被解析为函数。

从完全不同的角度来看,考虑Haskell,其中任何(二元)函数都可以被视为二元运算符:

如果你不懂Haskell,但了解点积,这个例子应该还是相当简单的。给定:

dotP :: (Double, Double) -> (Double, Double) -> Double
dotP (x1, y1) (x2, y2) = x1 * x2 + y1 * y2

以下两种写法都会得到11:

dotP (1,2) (3,4)

(1,2) `dotP` (3,4)

针对Go文档中的引用:Go开发人员只是强调,在C++中,我们会将new视为具有自己语法的关键字,而在Go中,我们应该将new视为任何其他函数。

英文:

The actual distinction between functions and operators depends on the programming language. In plain C, operators are a part of the language itself. One cannot add an operator, nor change the behavior of an existing operator. This is not the case with C++, where operators are resolved to functions.

From a totally different point of view, consider Haskell, where ANY (binary) function may be treated as a binary operator:

If you don't speak Haskell, but know about dot products, this example should still be fairly straight-forward. Given:

dotP :: (Double, Double) -> (Double, Double) -> Double
dotP (x1, y1) (x2, y2) = x1 * x2 + y1 * y2

Both

dotP (1,2) (3,4)

and

(1,2) `dotP` (3,4)

will give 11.

To address the quote in the Go documentation: The Go developers are simply stressing that where in C++, one would treat new as a keyword with its own syntax, one should treat new in Go as any other function.

答案2

得分: 1

虽然我仍然认为这个问题基本上是Difference between operator and function in C++?的重复,但是在你引用的特定上下文中澄清这个差异可能是值得的。

那里的重点是,在C++中,_函数_是具有名称和可能的函数参数的东西,并且使用以下语法调用:

func(arg1,arg2,...)

换句话说,先是名称,然后是圆括号,然后是逗号分隔的参数列表。这是C++的函数调用语法。

而_运算符_则是按照标准的第5条描述的方式使用。语法的细节取决于运算符的类型,例如有像&这样的一元运算符,像+*等的二元运算符;还有三元条件运算符? :,然后还有一些特殊的关键字,如newdeletesizeof,其中一些对于用户定义的类型来说是_转换_为函数调用,但它们不使用上面描述的_函数调用语法_。也就是说,你不会调用

new(arg1,arg2,...)

而是使用特殊的“一元表达式语法”(§5.3),其中暗示着,除其他事项外,关键字new后面没有圆括号(至少不是_必须的_)。

正是这种_语法_上的差异是作者在你引用的部分中谈论的。

英文:

Although I still think the question is basically a duplicate of Difference between operator and function in C++?, it may be worthwhile to clarify what the difference means in the specific context you quoted.

The point there is that a function in C++ is something that has a name and possibly function arguments, and is called using this syntax:

func(arg1,arg2,...)

In other words, the name first, then a round bracket, then the comma-separated list of arguments. This is the function call syntax of C++.

Whereas an operator is used in the way described by clause 5 of the Standard. The details of the syntax vary depending on the kind of operator, e.g. there are unary operators like &, binary operators like +, * etc.; there is also the ternary conditional operator ? :, and then there are special keywords like new, delete, sizeof, some of which translate to function calls for user-defined types, but they don't use the function call syntax described above. I.e. you don't call

new(arg1,arg2,...)

but instead, you use a special "unary expression syntax" (§5.3), which implies, among other things, that there are no round brackets immediately after the keyword new (at least, not necessarily).

It's this syntactic difference that the authors talk about in the section you quoted.

答案3

得分: 1

"运算符和函数之间有什么区别?"
语法。但实际上,这纯粹是一种与语言相关的约定:在C++中,+是一个中缀运算符(只有运算符可以是中缀),而func()是一个函数。但即使这也并不总是正确的:MyClass::operator+()是一个函数,但通常可以使用运算符语法来调用它。

其他语言有不同的规则:在像Lisp这样的语言中,没有真正的区别。可以区分内置函数和用户定义函数,但这种区别有些人为,因为你可以很容易地扩展Lisp以添加其他内置函数。还有一些语言允许使用中缀符号表示用户定义的函数。而像Python这样的语言在它们之间进行映射:lhs + rhs映射到函数调用lhs.__add__( rhs )(所以“运算符”实际上只是一种语法糖)。

总之,对于编程语言来说,并没有通用的规则。只是两个不同的词,每种语言都可以自由地使用它们来最好地描述该语言。

英文:

"What is the difference between operators and functions?"
Syntax. But in fact, it's purely a convention with regards to
the language: in C++, + is an infix operator (and only
operators can be infix), and func() would be a function. But
even this isn't always true: MyClass::operator+() is
a function, but it can, and usually is invoked using the
operator syntax.

Other languages have different rules: in languages like Lisp,
there is no real difference. One can distinguish between
built-in functions vs. user defined functions, but the
distinction is somewhat artificial, since you could easily
extend lisp to add additional built-in functions. And there are
languages which allow using the infix notation for user defined
functions. And languages like Python map between them: `lhs

  • rhsmaps to the function calllhs.add( rhs )` (so
    "operators" are really just syntactic sugar).

I sum, there is not rule for programming languages in general.
There are simply two different words, and each language is
free to use them as it pleases, to best describe the language.

答案4

得分: 1

在编程语言中,运算符和函数的确切区别是什么?

这是一个广泛的问题。在抽象语法树中,运算符是一元、二元或有时是三元节点 - 它们以一定的优先级将表达式绑定在一起,例如+的优先级低于*,而*的优先级又低于new

函数是一个更抽象的概念。作为一种原始类型,它们是带有类型的子程序入口点,根据语言的不同,可以作为右值在词法范围内使用。

C++允许通过将运算符的评估动态分派到相应的方法来重载运算符。这是一种语言的“特性”,正如这个问题的存在暗示的那样,它常常让人困惑,并且在Go中不可用。

英文:

> So what is the exact difference of operator vs function in programming languages in general?

It is broad. In an abstract syntax tree, operators are unary, binary or sometimes ternary nodes - binding expressions together with a certain precedence e.g. + has lower precedence than *, which in turn has lower precedence than new.

Functions are a much more abstract concept. As a primitive, they are typed subroutine entrypoints that depending on the language can be used as rvalues with lexical scope.

C++ allows to override (overload) operators with methods by means of dynamically dispatching operator evaluation to said methods. This is a language "feature" that - as the existence of this question implies - mostly confuses people and is not available in Go.

答案5

得分: 0

运算符是C++语言语法的一部分,在C++中,如果您不想使用默认行为,可以将它们作为函数进行“重载”。对于复杂类型或用户定义的类型,语言可能不知道运算符的语义,因此用户可以使用自己的实现对其进行重载。

英文:

operators are part of c++ language syntax, in C++ you may 'overload' them as functions if you dont want the default behaviour, For complex types or user defined types , Language may not have the semantic of the operator known , So yuser can overload them with thier own implementation.

huangapple
  • 本文由 发表于 2013年7月22日 16:06:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/17782638.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定