functions and overloading

Discussion in 'C++' started by Stefan Ram, Mar 10, 2010.

  1. Stefan Ram

    Stefan Ram Guest

    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?
     
    Stefan Ram, Mar 10, 2010
    #1
    1. Advertising

  2. Stefan Ram wrote:

    > 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.
     
    Johannes Schaub (litb), Mar 10, 2010
    #2
    1. Advertising

  3. Stefan Ram

    Stefan Ram Guest

    "Johannes Schaub (litb)" <> writes:
    >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).
     
    Stefan Ram, Mar 11, 2010
    #3
  4. Stefan Ram

    James Kanze Guest

    On Mar 10, 1:39 pm, -berlin.de (Stefan Ram) wrote:
    > 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?


    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

    --
    James Kanze
     
    James Kanze, Mar 11, 2010
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Xiangliang Meng
    Replies:
    1
    Views:
    1,610
    Victor Bazarov
    Jun 21, 2004
  2. Iyer, Prasad C

    Overloading __init__ & Function overloading

    Iyer, Prasad C, Sep 30, 2005, in forum: Python
    Replies:
    3
    Views:
    6,413
    Fredrik Lundh
    Sep 30, 2005
  3. Fredrik Lundh
    Replies:
    0
    Views:
    451
    Fredrik Lundh
    Sep 30, 2005
  4. Hicham Mouline
    Replies:
    0
    Views:
    435
    Hicham Mouline
    Apr 23, 2009
  5. Hicham Mouline
    Replies:
    1
    Views:
    421
    Michael DOUBEZ
    Apr 24, 2009
Loading...

Share This Page