functions and overloading

S

Stefan Ram

It once came to my mind that there are no functions, but just

- function definitions (in the source code),
- function declarations (in the source code),
- function call expressions (in the source code), and
- evaluation of function call expressions (at run time).

However, given an overloading, as in, say:

float cos( float );
long double cos( long double );

is this

- /one/ function »cos« with two different parameter lists, or
are these

- /two/ different functions »cos«, which happen to share the
same name?
 
J

Johannes Schaub (litb)

Stefan said:
It once came to my mind that there are no functions, but just

- function definitions (in the source code),
- function declarations (in the source code),
- function call expressions (in the source code), and
- evaluation of function call expressions (at run time).

Sure there are functions. They are listed as a type of entity in clause 3.
The following conditions give birth to functions:

- Function definition that's not nested in a template-declaration.
- Instantiation of a function template or member function of a class
template or member function of a nested class of a class template.
- Explicit specializations where all template arguments are non-dependent.

I find it somewhat difficult to determine whether a member function of a
class template is itself a template or not. I think it is not a template
declaration, but it gives birth to a template entity, instead of a function.
When it is instantiated, a function is born.

So, i think one can differentiate between a syntactical function (function
declaration), and a semantical function (declaration that declares a
function) (the latter being the entity). The former for example applies to
function definitions nested in template-declarations (which are function-
declarations, but not functions).

You may compare it to object declarations. The following two are object
declarations. But the entity they declare are not objects, but are
references. The characteristic of an object declaration (which makes it
different from function declarations) is that they may have an initializer.

extern int &a [ = ... ];
extern int (&b)() [ = ... ];

In an example in 14.6.1.1/1 the Standard says about three function
declarations that they declare three function templates. The former is the
syntactical kind, and the latter is the semantical kind.

I think this can sometimes be confusing, though.
However, given an overloading, as in, say:

float cos( float );
long double cos( long double );

is this

- /one/ function »cos« with two different parameter lists, or
are these

- /two/ different functions »cos«, which happen to share the
same name?

These are two functions that share the same name. These are overloaded
declarations.
 
S

Stefan Ram

Johannes Schaub (litb) said:
You may compare it to object declarations. The following two are object
declarations. But the entity they declare are not objects, but are

Thanks for the competent answer!

I must add at this point that it hurts my ears somewhat to
hear »object declaration«, although I know that this term is
used by ISO/IEC 14882:2003(E) itself.

A »declaration« - to me - is a source text unit that
introduces an identifier into a scope. An »object« to me is
not an identifier. Therefore, I would have called a
definition such as »int i;« not »object declarations« but
»object name declarations«, as it introduces a name for an
object into a scope (it also creates the object itself in
this case). A »using declaration« also introduces a name
into a scope, but never creates an object.

A »reference« - to me - also is just a name for an object.
It differs from a common variable by the way it was created,
but not afterwards. There also are some objects that can
only be named via a reference defintion (temporary objects).
 
J

James Kanze

It once came to my mind that there are no functions, but just
- function definitions (in the source code),
- function declarations (in the source code),
- function call expressions (in the source code), and
- evaluation of function call expressions (at run time).
However, given an overloading, as in, say:
float cos( float );
long double cos( long double );
- /one/ function cos with two different parameter lists, or
are these
- /two/ different functions cos , which happen to share the
same name?

Yes:).

Seriously, it depends on the definitions you use, but I'd say
that there are functions in the generated binary, and that it is
the function definition in the source code which causes them to
exist. So if the functions have different definitions, they are
two separate functions

But that's just the way I use the words
 

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

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top