gpt4 book ai didi

c++ - 模板外部链接?谁能解释一下?

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:48:57 27 4
gpt4 key购买 nike

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.

我知道使用关键字的外部链接

extern "C"

例如:

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

但是他们给了模板不得有C链接

上述声明的实际含义是什么?

谁能解释一下?

最佳答案

extern "C" 声明具有C 语言链接 的内容。这不同于外部链接内部链接。默认情况下,C++ 程序中的所有内容都具有 C++ 语言链接,但您可以通过指定 extern "C++" 来重申这一点。

外部链接 意味着名称对其他单独编译的源文件可见,前提是您包含正确的 header 或提供正确的声明。这允许您在 a.cpp 中定义函数 foo,并从 b.cpp 中调用它。 C++ 程序中命名空间范围内的大多数名称都具有外部链接。具有内部链接无链接 的异常(exception)。您可以通过指定 extern 将某些内容显式标记为具有外部链接。这与 extern "C" 不同。

internal linkage 表示该名称对当前编译单元是唯一的,您不能从另一个源文件访问变量或函数。声明为 static 的文件范围变量和函数具有内部链接。此外,默认情况下,使用常量表达式初始化的命名空间范围内的 const 整数变量具有内部链接,但您可以使用显式 extern 覆盖它。

最后,局部变量和类没有联系。这些名称对于它们在其中声明的函数是局部的,不能从该函数外部访问。您可以使用 extern 来指示您确实想要访问命名空间范围内的变量。

模板不能在本地范围内定义,但可以有内部或外部链接。

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
}

错误消息是正确的 --- 模板不能有 extern "C" 链接。

在基本层面上,模板不能有 extern "C" 链接,因为它们与 C 不兼容。特别是,模板不只是定义单个类或函数,而是定义一个家族共享相同名称但通过模板参数区分的类或函数。

只能声明一个具有给定名称的函数 extern "C"。当您考虑名称修改时,这是有道理的——在 C 中,函数 foo 通常在符号中被称为 foo_foo table 。在 C++ 中,foo 可能有很多重载,因此签名包含在符号表中的“损坏”名称中,您可能会得到 $3fooV foo$void 或其他东西来区分 foo(void)foo(int) 等等。在 C++ 中,标记为 extern "C" 的单个重载根据给定平台的 C 方案进行损坏,而其他重载保留其正常的损坏名称。

声明一个模板 extern "C" 需要所有实例化为 extern "C",因此与“只有一个具有给定名称的函数可以是 extern "C""规则。

尽管 C 没有对 struct 进行名称重整,但只能有一个具有给定名称的 struct。因此,禁止类模板使用 extern "C" 也是有道理的——一个模板定义了一组同名的类,所以哪个对应于 C struct?

关于c++ - 模板外部链接?谁能解释一下?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3341055/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com