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
can any one explain this ?
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
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:
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, but
And extern "C" is not what you think you know. 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
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?
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 struct
s, 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
?
In programming languages, particularly C++, linkage describes how names can or can not refer to the same entity throughout the whole program or one single translation unit.
The static keyword is used in C to restrict the visibility of a function or variable to its translation unit. This is also valid in C++, although C++ deprecates this usage in favor of anonymous namespaces (which are not available in C). Also, C++ implicitly treats any const namespace-scope variable as having internal linkage unless it is explicitly declared extern, unlike C.
A name's linkage is related to, but distinct from, its scope. The scope of a name is the part of a translation unit where it is visible. For instance, a name with global scope (which is the same as file-scope in C and the same as the global namespace-scope in C++) is visible in any part of the file. Its scope will end at the end of the translation unit, whether or not that name has been given external or internal linkage.
If the name has external linkage, the entity that name denotes may be referred to from another translation unit using a distinct declaration for that same name, and from other scopes within the same translation unit using distinct declarations. Were the name given internal linkage, such a declaration would denote a distinct entity, although using the same name, but its entity could be referred to by distinct declarations within the same translation unit. A name that has no linkage at all cannot be referred to from declarations in different scopes, not even from within the same translation unit. Examples of such names are parameters of functions and local variables. The details differ between C (where only objects and functions - but not types have linkage) and C++ and between this simplified overview.
Linkage between languages must be done with some care, as different languages adorn their external symbols differently.
**Linkage in C**
Definition of 'linkage' quoted from ISO/IEC 9899:TC3 (C99 Standard).
C uses the term "identifier" where this article uses name which is
what C++ uses to formalize
linkage:
An identifier declared in different scopes or in the same scope more than
once can be made to refer to the same object or function by a process
called linkage.[1]
The following is a common example of linkage:
/* file demo1.c */
/* extern / void foo(void); / extern optional - it's the default */
int main(void)
{
foo();
return 0;
}
/* file demo2.c */
void foo(void) { ... }
Function foo is declared in two files, with its function body defined in demo2.c. Via linkage, foo called in main() inside demo1.c refers to foo in demo2.c. This is an example of external linkage for a function.
If u didn't understood still..u can go through this....site...
this is same in http://en.wikipedia.org/wiki/Extern_%22C%22
u need more info
Extern template
In standard C++, the compiler must instantiate a template whenever a fully specified template is encountered in a translation unit. This can dramatically increase compile time, particularly if the template is instantiated in many translation units using the same parameters. There is no way to tell C++ not to provoke an instantiation of a template.
C++0x will introduce the idea of external templates. C++ already has syntax for forcing the compiler to instantiate at a particular location:
template class std::vector;
What C++ lacks is the ability to prevent the compiler from instantiating a template in a translation unit. C++0x will simply extend this syntax to:
extern template class std::vector;
This tells the compiler not to instantiate the template in this translation unit.
http://en.wikipedia.org/wiki/C%2B%2B0x#Extern_template
Example (C programming language)
File 1:
int GlobalVariable; // implicit definition void SomeFunction(); // function prototype (declaration)
int main() { GlobalVariable = 1; SomeFunction(); return 0; }
File 2:
extern int GlobalVariable; // explicit declaration
void SomeFunction() { // function header (definition) ++GlobalVariable; }
In this example, the variable GlobalVariable is defined in File 1. In order to utilize the same variable in File 2, it must be declared. Regardless of the number of files, a global variable is only defined once, however, it must be declared in any file outside of the one containing the definition.
again u need any info see this link ...... http://en.wikipedia.org/wiki/Extern