[英]How does “extern C++” work?
I jumped into winnt.h
and I found out the code as following: 我跳进了
winnt.h
,发现代码如下:
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? extern "C++"
如何工作? 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? 对于问题3,我的意思是我可以将模板的declearation和定义分开,然后为模板生成动态链接,而不使用这个技巧实际给出实现吗?
Well, extern "C++"
won't work in C, of course (though some compilers might support it as an extension). 那么,
extern "C++"
当然不适用于C(虽然有些编译器可能会支持它作为扩展)。 So it only makes sense to use it in C++. 所以在C ++中使用它才有意义。
That's because in the case of multiple nested extern linkage specifiers, the innermost one takes effect. 这是因为在多个嵌套的extern链接说明符的情况下,最里面的一个生效。 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. 因此,如果你有一个用
extern "C"
包围的头文件,你可以使用extern "C++"
暂时中断它并用C ++链接声明一些东西。
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++. 当你想为C ++库提供一个通用的C接口时,它是最有意义的,但你也想为在C ++中实际使用它的人提供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. 因此,你将
#ifdef __cplusplus \\ extern "C" { \\ #endif
整个标题放在标题周围,然后你将ifdef-in这些位用extern "C++"
恢复为C ++链接。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.