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 ?
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 ?
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:
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:Note that you can achieve a somehow similar effect as internal linkage by using anonymous namespaces.
Internal linkage: §3.5/2
Note that the difference is that it cannot be referred from other translation units.
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/2On the other hand, when you say that you:
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, likedlopen
and family. This is described in §7.5The 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 languageX
. It does not mean external linkage, so your original premise: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:
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?
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 specifyingextern "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
ina.cpp
, and call it fromb.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 specifyingextern
. This is distinct fromextern "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 explicitextern
.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.
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 functionfoo
is typically called eitherfoo
or_foo
in the symbol table. In C++ there may be many overloads offoo
, so the signature is incorporated in the "mangled" name in the symbol table, and you might get$3fooV
orfoo$void
or something else to distinguishfoo(void)
fromfoo(int)
and so forth. In C++, the single overload that is markedextern "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 beextern "C"
, which thus contradicts the "only one function with a given name can beextern "C"
" rule.Though C doesn't have name mangling for
struct
s, there can only be onestruct
with a given name. The ban onextern "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 Cstruct
?