Re: C++ reluctant to overload function

Discussion in 'C++' started by James Kanze, Oct 13, 2010.

  1. James Kanze

    James Kanze Guest

    On Oct 13, 6:21 pm, Juha Nieminen <> wrote:
    > Victor Bazarov <> wrote:
    > > On 10/12/2010 12:37 PM, Juha Nieminen wrote:
    > >> Victor Bazarov<> wrote:
    > >>> Name lookup when in a member function finds
    > >>> the member 'foo' which actually *hides* any other 'foo', and that's why
    > >>> the '<unnamed namespace>::foo' should NOT be found during that lookup
    > >>> and is *not* considered an overload.


    > >> What is the rationale for that? I can't immediately think
    > >> of a reason or notivation for that.


    > > Rationale for what, exactly? For the rules of hiding?
    > > Names defined in a closer scope hide names defined in the
    > > outer scope. It's an old rule, that AFAICT existed from the
    > > start in C++. For example,


    > foo(int) is not the same thing as foo(int,int). They are two different
    > functions with different signatures. In the original post one is hiding
    > the other. I can't understand the rationale for that.


    And double foo is not the same thing as int foo. Never the
    less, a declaration of double foo in an inner scope will hide
    a declaration of an int foo in an outer.

    It's purely an issue of name lookup. Once the compiler has
    found the name(s), it can decide whether the multiple
    declarations are legal or not. But any presence of a name in
    a nested scope hides all occurances of that name in outer
    scopes. (In the case of function calls, some additional names
    may be brought in by ADL. And templates obey somewhat special
    rules as well.)

    This is the rule in all languages with block scope that I know.
    It would be very confusing if, in C++, functions (and only
    functions) violated this rule---for that matter, I find the
    results of ADL confusing, largely because they do violate this
    rule.

    --
    James Kanze
     
    James Kanze, Oct 13, 2010
    #1
    1. Advertising

  2. James Kanze

    Steve Pope Guest

    Niklas Holsti <> wrote:

    >In Ada, if an inner scope defines a function
    >or procedure with the same name as a function or procedure in an outer
    >scope, the inner name hides the outer only if the parameter profiles
    >match, otherwise the inner overloads the outer.


    Thanks.

    I am curious then if Ada generally facilitated separate compilation, and
    subsequent linking, of modules, in a C/C++ manner.

    Steve
     
    Steve Pope, Oct 13, 2010
    #2
    1. Advertising

  3. Steve Pope wrote:
    > Niklas Holsti <> wrote:
    >
    >> In Ada, if an inner scope defines a function
    >> or procedure with the same name as a function or procedure in an outer
    >> scope, the inner name hides the outer only if the parameter profiles
    >> match, otherwise the inner overloads the outer.

    >
    > Thanks.
    >
    > I am curious then if Ada generally facilitated separate compilation, and
    > subsequent linking, of modules, in a C/C++ manner.


    Yes, certainly. An Ada program is usually divided into several modules,
    or compilation units. A compilation unit is usually a "package", which
    is a bit like a C++ namespace, but it can also be a single procedure or
    function.

    Each module is usually represented by two source files: a specification
    file, analogous to a C/C++ header file, and a body (implementation)
    file. Both specification files and body files are compiled separately.

    The compilation-unit structure is a core part of the Ada syntax, not a
    preprocessor feature like #include. Consequently most Ada compilers have
    a simple "build" command to (re)build the program's executable by
    compiling and linking whatever compilation units are needed. Makefiles
    are usually unnecessary.

    (Apologies for advertising Ada on a C++ forum, but the question was
    asked :)

    --
    Niklas Holsti
    Tidorum Ltd
    niklas holsti tidorum fi
    . @ .
     
    Niklas Holsti, Oct 13, 2010
    #3
  4. James Kanze

    Steve Pope Guest

    Niklas Holsti <> wrote:

    >Steve Pope wrote:


    >> Niklas Holsti <> wrote:


    >>> In Ada, if an inner scope defines a function
    >>> or procedure with the same name as a function or procedure in an outer
    >>> scope, the inner name hides the outer only if the parameter profiles
    >>> match, otherwise the inner overloads the outer.

    >>
    >> Thanks.
    >>
    >> I am curious then if Ada generally facilitated separate compilation, and
    >> subsequent linking, of modules, in a C/C++ manner.

    >
    >Yes, certainly. An Ada program is usually divided into several modules,
    >or compilation units. A compilation unit is usually a "package", which
    >is a bit like a C++ namespace, but it can also be a single procedure or
    >function.
    >
    >Each module is usually represented by two source files: a specification
    >file, analogous to a C/C++ header file, and a body (implementation)
    >file. Both specification files and body files are compiled separately.
    >
    >The compilation-unit structure is a core part of the Ada syntax, not a
    >preprocessor feature like #include. Consequently most Ada compilers have
    >a simple "build" command to (re)build the program's executable by
    >compiling and linking whatever compilation units are needed. Makefiles
    >are usually unnecessary.


    >(Apologies for advertising Ada on a C++ forum, but the question was
    >asked :)


    Thanks. I have not really looked at Ada for 30 years. (Literally,
    I studied it for a bit in 1980.)


    Steve
     
    Steve Pope, Oct 13, 2010
    #4
  5. Steve Pope wrote:
    > Niklas Holsti <> wrote:
    >
    >> Steve Pope wrote:

    > ...
    >>> I am curious then if Ada generally facilitated separate compilation, and
    >>> subsequent linking, of modules, in a C/C++ manner.

    >> Yes, certainly....

    >
    > Thanks.


    You are welcome, Steve.

    > I have not really looked at Ada for 30 years. (Literally,
    > I studied it for a bit in 1980.)


    Nowadays the GNU Ada compiler GNAT makes it easy and cheap to use Ada.
    See, for example, http://libre.adacore.com/. Also, comp.lang.ada is
    alive and active.

    --
    Niklas Holsti
    Tidorum Ltd
    niklas holsti tidorum fi
    . @ .
     
    Niklas Holsti, Oct 14, 2010
    #5
    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. Replies:
    7
    Views:
    517
  2. Replies:
    4
    Views:
    572
  3. Daniel Bickett

    Help! Host is reluctant to install Python

    Daniel Bickett, Jan 25, 2005, in forum: Python
    Replies:
    7
    Views:
    328
  4. Steve Pope
    Replies:
    43
    Views:
    1,020
    Victor Bazarov
    Oct 15, 2010
  5. Ying-Chieh Liao

    function overload (not operator overload)

    Ying-Chieh Liao, Oct 11, 2004, in forum: Perl Misc
    Replies:
    3
    Views:
    255
    Sherm Pendley
    Oct 11, 2004
Loading...

Share This Page