linkage of free functions

Discussion in 'C++' started by Angel Tsankov, Apr 11, 2006.

  1. Considering the following declarations

    void f1( );
    namespace {
    void f2( );
    }

    both f1 and f2 have external linkage, but may f2 be referred to by names from scopes of other translation units?

    If so, how? If no, then this constradicts the definition of "a name with external linkage" (3.5.2):

    When a name has external linkage, the entity it denotes can be referred to by names from scopes of
    other translation units or from other scopes of the same translation unit.
    Angel Tsankov, Apr 11, 2006
    #1
    1. Advertising

  2. Angel Tsankov wrote:
    > Considering the following declarations
    >
    > void f1( );
    > namespace {
    > void f2( );
    > }
    >
    > both f1 and f2 have external linkage, but may f2 be referred to by names
    > from scopes of other translation units?


    According to the C++ standard, it can't be referenced directly, however
    it can be referred to by an exported template.

    >
    > If so, how? If no, then this constradicts the definition of "a name with
    > external linkage" (3.5.2):
    > int x;


    namespace {
    int y;
    };

    static int z;

    template <int & p>
    void f();

    int main()
    {
    f<x>();
    f<y>();
    f<z>();
    }

    > When a name has external linkage, the entity it denotes can be referred
    > to by names from scopes of
    > other translation units or from other scopes of the same translation unit.



    In theory, template f below can only have a parameter with "external"
    linkage. Even though "y" is in an anonymous namespace, it has
    "external" linkage.

    int x;

    namespace {
    int y;
    };

    static int z; // not external

    template <int & p>
    void f();

    int main()
    {
    f<x>();
    f<y>();
    f<z>();
    }
    Gianni Mariani, Apr 11, 2006
    #2
    1. Advertising

  3. Angel Tsankov

    Tom Widmer Guest

    Angel Tsankov wrote:
    > Considering the following declarations
    >
    > void f1( );
    > namespace {
    > void f2( );
    > }
    >
    > both f1 and f2 have external linkage, but may f2 be referred to by names
    > from scopes of other translation units?


    Not directly, no.

    > If so, how? If no, then this constradicts the definition of "a name with
    > external linkage" (3.5.2):
    >
    > When a name has external linkage, the entity it denotes can be referred
    > to by names from scopes of
    > other translation units or from other scopes of the same translation unit.


    Well, it's clear enough given 7.3.1.1/1, though I agree that the
    definition of external linkage could be improved. In practice it's a
    name that the linker sees, though obviously the standard doesn't talk
    about linkers.

    Tom
    Tom Widmer, Apr 12, 2006
    #3
  4. Angel Tsankov

    Pete Becker Guest

    Tom Widmer wrote:
    > Angel Tsankov wrote:
    >
    >> Considering the following declarations
    >>
    >> void f1( );
    >> namespace {
    >> void f2( );
    >> }
    >>
    >> both f1 and f2 have external linkage, but may f2 be referred to by
    >> names from scopes of other translation units?

    >
    >
    > Not directly, no.
    >


    A uesless answer is actually yes: you could refer to it, except that you
    don't know the name of the enclosing namespace. That's why it's still
    meaningful to talk about the name having external linkage without
    talking about the linker.

    --

    Pete Becker
    Roundhouse Consulting, Ltd.
    Pete Becker, Apr 12, 2006
    #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. Mary
    Replies:
    1
    Views:
    3,978
    Thomas Fritsch
    Oct 11, 2004
  2. qazmlp
    Replies:
    0
    Views:
    341
    qazmlp
    Jul 17, 2003
  3. Bill Pursell

    simulating private member functions--static linkage

    Bill Pursell, Apr 27, 2006, in forum: C Programming
    Replies:
    4
    Views:
    327
    Michael Wojcik
    May 1, 2006
  4. Nagrik
    Replies:
    5
    Views:
    471
    James Kanze
    Nov 28, 2007
  5. Replies:
    1
    Views:
    579
    Michael DOUBEZ
    Sep 12, 2008
Loading...

Share This Page