Difference between operator and function

A

Alf P. Steinbach

* Alex Vinokur:
What is the difference between an operator and a function in C++?

An operator is a special kind of function, with a non-alphanumeric name,
an operator symbol, which can be invoked using prefix, infix or postfix
notation, e.g.

-x // prefix unary minus '-'
a < b // infix less than '<'
functioid( 3.14 ) // Postfix function call operator '()'

To define an operator you use the word 'operator' followed by the
operator symbol.

E.g.,

bool operator<( Thing const& a, Thing const& b )
{
return a.isLessThan( b );
}

There are restrictions on defining some operators.
 
J

Jack Klein

* Alex Vinokur:

An operator is a special kind of function, with a non-alphanumeric name,
an operator symbol, which can be invoked using prefix, infix or postfix
notation, e.g.

The concept of "special kind of function" is a very strange one, and
certainly not supported by any wording in the C++ language.

The notion that operators must have a non-alphanumeric name is quite
easily refuted by the following operators:

- sizeof
- const_cast
- static_cast
- dynamic_cast
- reinterpret_cast
 
J

Jack Klein

What is the difference between an operator and a function in C++?

Alex Vinokur
email: alex DOT vinokur AT gmail DOT com
http://mathforum.org/library/view/10978.html
http://sourceforge.net/users/alexvn

I read, and seriously disagree with, Alf's reply.

Here is the difference as I see it:

Operators are defined as operators by the language. Some operators
are overloadable, others are not. Most operators do not create
sequence points (although when overloaded, the overloaded versions are
functions and do impose sequence points). The number of operands on
which an operator operates is fixed and immutable.

Other than the fact that functions are overloadable, none of the above
applies to them.
 
A

Alf P. Steinbach

* Jack Klein:
The concept of "special kind of function" is a very strange one,

No, it's not a strange concept, it's the basic idea.

The standard calls it an "operator function" (some operator functions
can be user-defined).

and
certainly not supported by any wording in the C++ language.

Yes, the standard does not AFAIK define the general concept of
operators. AFAIK there's not even a list of operators, except if a
table with punctuation symbols thrown in is OK. The reader is simply
supposed to already have a working notion of what operators are.

The notion that operators must have a non-alphanumeric name is quite
easily refuted by the following operators:

- sizeof
- const_cast
- static_cast
- dynamic_cast
- reinterpret_cast

In addition to those there's 'new', 'delete', 'and', 'and_eq', 'bitand',
'bitor', 'compl', 'not', 'not_eq', 'or', 'or_eq', 'xor' and 'xor_eq'.

So clearly an operator mustn't necessarily have a non-alphanumeric name.

But operators /usually/ are non-alphanumeric (those I listed here,
except the two first, are in practice not used, and those you listed
cannot be overridden, so these alphanumeric operators are very special
cases), and since they usually are, they /can/ be non-alphanumeric.
 
A

Alex Vinokur

Jack Klein said:
Most operators do not create sequence points (although when overloaded, the overloaded versions are functions and do impose
sequence points).
[snip]

What does "sequence points" mean?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,066
Latest member
VytoKetoReviews

Latest Threads

Top