Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > linkage of free functions

Reply
Thread Tools

linkage of free functions

 
 
Angel Tsankov
Guest
Posts: n/a
 
      04-11-2006
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.
 
Reply With Quote
 
 
 
 
Gianni Mariani
Guest
Posts: n/a
 
      04-11-2006
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>();
}
 
Reply With Quote
 
 
 
 
Tom Widmer
Guest
Posts: n/a
 
      04-12-2006
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
 
Reply With Quote
 
Pete Becker
Guest
Posts: n/a
 
      04-12-2006
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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
c++ linkage vs c linkage ramasubramanian.rahul@gmail.com C++ 1 09-12-2008 11:41 AM
linkage of static and inline functions Nagrik C++ 5 11-28-2007 11:00 AM
simulating private member functions--static linkage Bill Pursell C Programming 4 05-01-2006 03:44 PM
JNI problem - linkage problems trying to generate DLL for Win32 functions... Mary Java 1 10-11-2004 12:31 PM
linkage of class member functions qazmlp C++ 0 07-17-2003 05:05 AM



Advertisments