template External Linkage ?can anyone Explain this

2019-03-20 20:51发布

问题:

A template name has linkage (3.5). A non-member function template can have internal linkage; any other template name shall have external linkage. Entities generated from a template with internal linkage are distinct from all entities generated in other translation units.

I know about external linkage using the keyword

extern "C"

EX :

extern "C" {   template<class T>  class X { };   }

but they gave template shall not have a C linkage

what actually meant for the above statement?

can any one explain this ?

回答1:

extern "C" declares something to have C language linkage. This is different from external linkage and internal linkage. By default, everything in a C++ program has C++ language linkage, though you can reiterate that by specifying extern "C++".

external linkage means that the name is visible to other source files compiled separately, assuming you include the right headers or provide the right declarations. This is what allows you to define a function foo in a.cpp, and call it from b.cpp. Most names at namespace scope in a C++ program have external linkage. The exceptions are those that have internal linkage, and those that have no linkage. You can explicitly mark something as having external linkage by specifying extern. This is distinct from extern "C".

internal linkage means that the name is unique to the current compilation unit, and you cannot access the variable or function from another source file. File scope variables and functions declared static have internal linkage. Also, const integer variables at namespace scope that are initialized with a constant expression have internal linkage by default, though you can override it with an explicit extern.

Finally, local variables and classes have no linkage. The names are local to the function they are declared in, and cannot be accessed from outside that function. You can use extern to indicate that you really want to access a variable at namespace scope.

Templates cannot be defined at local scope, but may have either internal or external linkage.

int i; // namespace scope variable has external linkage
extern int j; // explicitly mark j with external linkage
static int k; // k has internal linkage
int const n=42; // internal linkage
extern int const m=99; // external linkage

void foo(); // foo has external linkage; it may be defined in this source file or another
extern void foo(); // explicitly mark foo with external linkage
static void bar(); // bar has internal linkage, and must be defined in this source file

void foo(){} // definition of foo, visible from other source files
void bar(){} // definition of bar, not visible from other source files (internal linkage)

static void baz(){} // declare and define baz with internal linkage

template<typename T> void foobar(){} // foobar has external linkage
template<typename T>
static void foobaz(){} // foobaz has internal linkage

void wibble()
{
    int i; // local, no linkage
    extern int i; // references i, declared above with external linkage
}

extern "C"
{
    int i2; // namespace scope variable has external linkage, and "C" linkage
    extern int j2; // explicitly mark j2 with external linkage and "C" linkage
    static int k2; // k2 has internal linkage and "C" linkage
    int const n2=42; // internal linkage and "C" linkage
    extern int const m2=99; // external linkage and "C" linkage

    void foo2(); // foo2 has external linkage and "C" linkage
    static void bar2(); // bar2 has internal linkage and "C" linkage

    void foo2(){} // definition of foo2, still with external linkage and "C" linkage
    void bar2(){} // definition of bar2, still with internal linkage and "C" linkage

    static void baz(){} // declare and define baz with internal linkage
}

The error message is correct --- templates cannot have extern "C" linkage.

At the basic level, templates cannot have extern "C" linkage because they are not compatible with C. In particular, a template doesn't just define a single class or function, but a family of classes or functions that share the same name, but are distinguished by their template parameters.

Only one function with a given name may be declared extern "C". This makes sense when you think about the name mangling --- in C, a function foo is typically called either foo or _foo in the symbol table. In C++ there may be many overloads of foo, so the signature is incorporated in the "mangled" name in the symbol table, and you might get $3fooV or foo$void or something else to distinguish foo(void) from foo(int) and so forth. In C++, the single overload that is marked extern "C" gets mangled according to the C scheme for the given platform, whereas the other overloads keep their normal mangled name.

Declaring a template extern "C" would require all instantiations to be extern "C", which thus contradicts the "only one function with a given name can be extern "C"" rule.

Though C doesn't have name mangling for structs, there can only be one struct with a given name. The ban on extern "C" for class templates thus also makes sense --- a template defines a family of classes with the same name, so which one corresponds to the C struct?



回答2:

Just by reading carefully the quote you wrote you will notice that, except non-member function templates that might have internal linkage, all other templates have external linkage. There is no need to add keywords, nor keywords can be added there.

The description of what linkage means is in §3.5/2, in particular external linkage is defined as:

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.

To force internal linkage of a template non-member function you can use the static keyword, but you cannot do the same with other templates:

template <typename T>
static void foo( T ) {}

Note that you can achieve a somehow similar effect as internal linkage by using anonymous namespaces.

Internal linkage: §3.5/2

When a name has internal linkage, the entity it denotes can be referred to by names from other scopes in the same translation unit.

Note that the difference is that it cannot be referred from other translation units.

namespace {
   template <typename T>
   class test {};
}

While the unnamed namespace does not make the linkage internal, it ensures that there will be no name collision as it will be in a unique namespace. This uniqueness guarantees that the code is not accessible from other translation units. Unnamed namespaces are considered to be a better alternative to the static keyword §7.3.1.1/2

The use of the static keyword is deprecated when declaring objects in a namespace scope (see annex D); the unnamed-namespace provides a superior alternative

On the other hand, when you say that you:

know about external linkage using the keyword extern "C"

You don't. extern "C" is not a request for external linkage. Reread the spec. extern "C" is a linkage-specification and instructs the compiler to use "C" style linkage within the block to interact with C code or libraries that already work that way, like dlopen and family. This is described in §7.5



回答3:

extern "C" is used to change symbol name of C++ function in order to use them from a C program.

In C++, function prototype is "coded" in symbol name, this is a requirement for overloading. But in C, you don't have a such feature.

extern "C" allow to call C++ function from a C program.

extern "C" is not what you are looking for.

Could you please explain what do you want to do ?



回答4:

The answer to the updated question is, as I already said in the answer that applies to the original question, that you are misunderstanding what extern "C" means.

The sequence extern "X" allows you to change the language linkage of the following function or block to language X. It does not mean external linkage, so your original premise:

I know about external linkage using the keyword extern "C"

is false. You don't know what it means. Refer to 7.5 in the standard. Language linkage affects how the compiler processes parameters and whether it applies (and potentially how) name mangling to the symbols.

Taking aside your insistence in that particular error, the compiler is complaining about your code because it is invalid according to the standard. In particular §14[temp]/4:

A template name has linkage (3.5). A non-member function template can have internal linkage; any other template name shall have external linkage. Entities generated from a template with internal linkage are distinct from all entities generated in other translation units. A template, a template explicit specialization (14.7.3), or a class template partial specialization shall not have C linkage. If the linkage of one of these is something other than C or C++, the behavior is implementation-defined. Template definitions shall obey the one definition rule (3.2). [Note: default arguments for function templates and for member functions of class templates are considered definitions for the purpose of template instantiation (14.5) and must also obey the one definition rule.]

I really think that before trying to evaluate how different compilers comply with the standard you should take your time to understand the standard. It is quite fine to have questions, and there is people making an effort to answer them. Showing that you have read the answers and tried to grasp what they mean is just a sign of respect. What part of the last paragraph in the previous answer here is unclear? Did you read it? Did you understand it? If you didn't, why did you not ask in a comment to the answer?