I jumped into winnt.h
and I found out the code as following:
extern "C++" // templates cannot be declared to have 'C' linkage
template <typename T, size_t N>
char (*RtlpNumberOf( UNALIGNED T (&)[N] ))[N];
I'd like to ask questions as following:
extern "C++"
work?With question 3, I mean that can I separate declearation and definition of the templates, and then generate a dynamic link for the template without actually give the implementation by using this trick?
“extern” keyword is used to extend the visibility of function or variable. By default the functions are visible throughout the program, there is no need to declare or define extern functions. It just increase the redundancy. Variables with “extern” keyword are only declared not defined.
Using extern "C" lets the compiler know that we want to use C naming and calling conventions. This causes the compiler to sort of entering C mode inside our C++ code. This is needed because C++ compilers mangle the names in their symbol table differently than C compilers and hence behave differently than C compilers.
the extern keyword is used to extend the visibility of variables/functions. Since functions are visible throughout the program by default, the use of extern is not needed in function declarations or definitions. Its use is implicit.
The extern “C” keyword is used to make a function name in C++ have the C linkage. In this case the compiler does not mangle the function.
Well, extern "C++"
won't work in C, of course (though some compilers might support it as an extension). So it only makes sense to use it in C++.
That's because in the case of multiple nested extern linkage specifiers, the innermost one takes effect. So if you have a header file surrounded with extern "C"
, you can use extern "C++"
to temporarily break out of it and declare something with C++ linkage.
It makes the most sense when you want to provide a generally C interface for a C++ library, but you also want to provide C++ helper bits for people actually using it in C++. So you'd put #ifdef __cplusplus \ extern "C" { \ #endif
around the header as a whole, and then you ifdef-in those bits with extern "C++"
to revert to C++ linkage.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With