Where in C++17 document say that class static member function has external linkage?

170 views Asked by At

I've seen some statements that static member function has external linkage.

However, I cannot find the statement from C++17 official document. Does anyone know?

If anyone provides the line from c++17 document or any other version that states static member function has external linkage, will be truly appreciated.

[Edit]

Perhaps question is not answerable because it depends on situation. What I don't understand and want to understand is how to identify which has internal or external linkage.

What it means by "depends on situation" is something like,

"In addition, a member function, static data member, a named class or enumeration of class scope, or an unnamed class or enumeration defined in a class-scope typedef declaration such that the class or enumeration has the typedef name for linkage purposes (10.1.3), has the same linkage, if any, as the name of the class of which it is a member. (ISO N4659 6.5.5)"

It doesn't mention static member "function". So I'm not sure it applies the same.

This is example case.

[Example 1]

A.h

class A
{
public:

    A();
    ~A();
};

A.cpp

#include "A.h"

A::A()
{
}

A::~A()
{
}

The class A has external linkage because it belongs to

  • a namespace (global), which is not unnamed namespace.
  • "All other namespaces have external linkage" (ISO N4659 6.5.4)

Such as A constructor is "visible" to different cpps as long as header is included shows it is external linkage, which is "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" (ISO N4659 6.5.2.1).

[Example 2]

A.h

#pragma once
class A
{
public:

    static void Foo()
    {
    }

    A(){}
    ~A(){}
};

B.h

#pragma once
class B
{
public:

    void Bar();

    B();
    ~B();
};

B.cpp

#include "B.h"
#include "A.h"

void B::Bar()
{
    A::Foo();
}

B::B()
{
}

B::~B()
{
}

C.h

#pragma once
class C
{
public:

    void Bar();

    C();
    ~C();
};

C.cpp

#include "C.h"
#include "A.h"

void C::Bar()
{
    A::Foo();
}

C::C()
{
}

C::~C()
{
}

main.cpp

#include <iostream>
#include "B.h"
#include "C.h"

int main()
{
    B b;
    C c;
    b.Bar();
    c.Bar();

    return 0;
}

The class A has external linkage for same reason in example 1. The static member function Foo in A has external linkage because

"Member functions of a class in namespace scope have the linkage of that class. Member functions of a local class (12.4) have no linkage." (ISO N4659 12.2.1.2)

It doesn't explicitly mention "static" member function belongs to this "member function" in this statement.

In short, from example 2,

  • class A has external linkage because

"All other namespaces have external linkage" (ISO N4659 6.5.4)

  • static void Foo has external linkage because

"In addition, a member function, static data member, a named class or enumeration of class scope, or an unnamed class or enumeration defined in a class-scope typedef declaration such that the class or enumeration has the typedef name for linkage purposes (10.1.3), has the same linkage, if any, as the name of the class of which it is a member. (ISO N4659 6.5.5)"

"Member functions of a class in namespace scope have the linkage of that class. Member functions of a local class (12.4) have no linkage." (ISO N4659 12.2.1.2)

Am I understanding correctly?

1

There are 1 answers

0
Brian Bi On

The wording in the C++17 standard can be found in [basic.link]/5:

In addition, a member function, static data member, a named class or enumeration of class scope, or an unnamed class or enumeration defined in a class-scope typedef declaration such that the class or enumeration has the typedef name for linkage purposes (10.1.3), has the same linkage, if any, as the name of the class of which it is a member.

A static member function is a member function, so if the class name has external linkage, then the name of a static member function of the class also has external linkage. But class names can also have internal linkage or no linkage (and in C++20, they can also have module linkage).