本文介绍了模板编译的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有什么可能是一个愚蠢的问题,但我只是不理解我的书的解释。

I have what could possibly be a dumb question, but I'm just not understanding my book's explanation for it.

它说当编译器看到模板的定义时,它不会生成代码。它只会在我们实例化一个特定的事实上,只有当我们使用模板(而不是当我们定义它时)生成代码的事实影响我们如何组织我们的源代码,当检测到错误...要生成实例化,编译器需要有因此,与非模板代码不同,模板的头文件通常包括定义和声明。

It says "When the compiler sees the definition of a template, it does not generate code. It generates code only when we instantiate a specific instance of the template. The fact that code is generated only when we use a template (and not when we define it) affects how we organize our source code and when errors are detected...To generate an instantiation, the compiler needs to have the code that defines a function template or class template member function. As a result, unlike non-template code, headers for templates typically include definitions as well as declarations."

生成代码到底是什么意思?我想我从来没有真正考虑过编译器是什么,并不真正了解它,所以我不知道什么是不同的,当你编译模板函数或类比非模板的时候。 p>

What exactly does it mean by "generate code"? I guess I've never really thought about what is going on with the compiler and don't really know much about it, so I'm not understanding what is different when you compile template functions or classes compared to non-template ones

推荐答案

编译器 生成 模板中给定的特定类型的代码类实例化。

The compiler generates the code for the specific types given in the template class instantiation.

如果你有一个模板类声明为

If you have for instance a template class declaration as

template<typename T>
class Foo
{
public:
     T& bar()
     {
         return subject;
     }
private:
     T subject;
};
Foo<int> fooInt;
Foo<double> fooDouble;

这些将有效地生成 代码,因为你会定义类

these will effectively generate the same linkable code as you would have defined classes like

class FooInt
{
public:
     int& bar()
     {
         return subject;
     }
private:
     int subject;
}

class FooDouble
{
public:
     double& bar()
     {
         return subject;
     }
private:
     double subject;
}

并实例化

FooInt fooInt;
FooDouble fooDouble;

关于模板(不要混淆无论模板如何)需要被看到与头文件很清楚为什么:

编译器无法生成此代码,而没有看到。它可以指在连接阶段首先出现的匹配实例化。

Regarding the point that template definitions (don't confuse with declarations regardless of templates) need to be seen with the header (included) files, it's pretty clear why:
The compiler can't generate this code without seeing the definition. It can refer to a matching instantiation that appeared first at linking stage though.

UPDATE:

非模板类/成员/函数的声明为链接器提供了一个预定义的入口点。该定义可以从编译目标文件中看到的单个实现中绘制(== .cpp == )。

相反,模板类/成员/函数的声明可以从给定相同或不同模板参数的任意编译单元进行实例化。这些模板参数的定义需要至少被看到一次。

The declaration of a non-template class/member/function gives a predefined entry point for the linker. The definition can be drawn from a single implementation seen in a compiled object file (== .cpp == compilation unit).
In contrast the declaration of a templated class/member/function might be instantiated from arbitrary compilation units given the same or varying template parameters. The definition for these template parameters need's to be seen at least once. It can be either generic or specialized.

请注意,您可以将特定类型的模板实现专门化(包括在标题中或特定的)。
如果您要在,并且不要使用非特殊类型的模板类,这也应该足以将它们链接在一起。

Note that you can specialize template implementations for particular types anyway (included with the header or at a specific compilation unit).If you would provide a specialization for your template class in one of your compilation units, and don't use your template class with types other than specialized, that also should suffice for linking it all together.

我希望这个示例有助于澄清编译器的差异和所做的努力。

I hope this sample helps clarifying what's the difference and efforts done from the compiler.

这篇关于模板编译的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-24 07:55