模板函数和类在不同文件中的使用

发布于 2024-11-03 07:46:16 字数 179 浏览 1 评论 0原文

我想要在一个文件中定义一个模板函数并在多个文件中使用。这与常规函数原型的工作方式相同吗?那么我可以定义一次并将原型包含在其他文件中吗?我对类有同样的问题,是否必须在每个头文件中包含模板类的完整定义,就像对类一样?如果我在单独的文件中两次定义模板函数,是否会导致错误,或者是否会未经检查。

还有一个问题,模板函数原型的格式是什么?

I want to have a template function defined in one file and used in many files. Does this work the same way regular function prototypes work? So I can define it once and just include the prototype in other files? I have the same question for classes, must I include the full defintion of a template class in each header file, just as I would for a class? Would it cause and error if I defined a template function twice in separate files or would this just go unchecked.

One more question, what is the format for a template function prototype?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

┼── 2024-11-10 07:46:16

不,它与常规函数不同。对于常规函数,您可以

void foo(int);
void foo(double);

在标头中声明,在某些源文件中定义函数,例如 foo.cc,#include 标头到任何必须使用这些函数的源文件中,例如bar.cc,然后让链接器完成剩下的工作。编译器将编译 bar.cc 并生成 bar.o,确信您已经在某处定义了函数,如果没有,您将获得一个链接-时间错误。

但如果您使用模板:

template <typename T>
void foo(T) ...

尝试想象它会如何工作。源文件 foo.ccbar.cc 是独立的,彼此一无所知,除了它们都同意 #include 的标头中的内容(这就是整个主意)。所以 bar.cc 不知道 foo.cc 是如何实现的,而 foo.cc 也不知道 bar 是什么。 cc 将处理这些函数。在这种情况下,foo.cc 不知道 bar.cc 将为 T 指定什么类型。那么,foo.cc 怎样才能为世界上的每个类型名提供定义呢?

它不能,所以这种方法是不允许的。您必须在标头中包含整个模板,以便编译器可以为 foo(int)、或 foo(string)foo 定义(myWeirdClass),或任何 bar.cc 调用的内容,并将其构建到 bar.o 中(或者如果模板对该类型没有意义,则抱怨) 。

课程也是如此。

模板专业化的规则略有不同,但在尝试高级技术之前,您应该很好地掌握基础知识。

No, it's not the same as a regular function. With a regular function, you can declare

void foo(int);
void foo(double);

in a header, define the functions in some source file, such as foo.cc, #include the header in any source file that has to use those functions, such as bar.cc, and let the linker do the rest. The compiler will compile bar.cc and produce bar.o, confident that you've defined the functions somewhere, and if you haven't then you'll get a link-time error.

But if you're using a template:

template <typename T>
void foo(T) ...

try to imagine how that would work. The source files foo.cc and bar.cc are independent and know nothing about each other, except that they agree on what's in the headers they both #include (that's the whole idea). So bar.cc doesn't know how foo.cc implements things, and foo.cc doesn't know what bar.cc will do with these functions. In this scenario, foo.cc doesn't know what type bar.cc will specify for T. So how can foo.cc possible have definitions for every typename under the sun?

It can't, so this approach isn't allowed. You must have the whole template in the header, so that the compiler can gin up a definition for foo(int), or foo(string), or foo(myWeirdClass), or whatever bar.cc calls for, and build it into bar.o (or complain if the template makes no sense for that type).

The same goes for classes.

The rules are a little different for template specializations, but you should get a good grip on the basics before trying the advanced techniques.

倾城泪 2024-11-10 07:46:16

请参阅此常见问题解答。特别是,第 12、13 和 14 项涉及分离模板函数的声明和定义。

See this FAQ. Especially, items 12, 13 and 14 deals with separating the declaration and definition of template functions.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文