linkage of free functions

A

Angel Tsankov

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.
 
G

Gianni Mariani

Angel said:
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>();
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>();
}
 
T

Tom Widmer

Angel said:
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
 
P

Pete Becker

Tom said:
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.
 

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,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top