模板方法在哪里实例化? [英] Where are template methods instantiated?

查看:53
本文介绍了模板方法在哪里实例化?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为了将不完整类型与智能指针一起使用,例如 boost :: scoped_ptr ,必须在相应的CPP文件中为父类显式定义一个空的析构函数.示例:

In order to use an incomplete type with a smart pointer such as boost::scoped_ptr, one must explicitly define an empty destructor for the parent class in the corresponding CPP file. Example:

// H file
class Foo
{
public:
  ~Foo();

private:
  class Pimpl;
  boost::scoped_ptr<Pimpl> pimpl_;
};

// CPP file
class Foo::Pimpl {};

Foo::~Foo() {}

编译器究竟将 boost :: scoped_ptr 的析构函数的实例放在哪里?我试图从视觉上想象这两个源文件中的哪一个,就像我自己已明确定义scoped_ptr的析构函数一样.用这种方式想象是合理的吗?

Where exactly does the compiler place the instantiation of boost::scoped_ptr's destructor? I'm trying to visually imagine where it would be in either of these source files, as if I had explicitly defined scoped_ptr's destructor myself. Is it reasonable to imagine it this way?

我知道在使用某种类型之前,不会为该类型实例化模板方法和类,但是我现在试图从结构上考虑它,以及如果它实际上是手工编写的,则编译器会将其放置在何处它.这将帮助我更好地了解如何使用这些技术(如上述技术).

I know that template methods and classes aren't instantiated for a type until it is used with that type, but I'm trying to think about it structurally now, and where the compiler would place it if it actually hand-wrote it. This will help me get a better understanding of how such techniques (like the one above) are able to be used.

我也不确定模板类的整个定义是实例化的,还是仅实例化了模板类的相关部分(因为使用了它们).换句话说,是否可能仅存在 boost :: scoped_ptr 的整个定义的一部分?

Also I'm not sure if the entire definition of a template class is instantiated or only the relevant parts of it, as they are used. In other words, is it possible for only part of boost::scoped_ptr's entire definition to exist?

推荐答案

我觉得我在您的问题中遗漏了一些东西.

I feel like I'm missing something from your question.

在此示例中,仅使用一个主体(".cpp")文件,因此它就在那里.更准确地说,它与析构函数进入相同的目标文件(".o",.obj"等).语言标准没有具体说明,但这就是我所熟悉的所有实现方式.

There's only one body (".cpp") file being used in this example, so it goes there. More precisely, it goes into the same object file (".o", ".obj", etc) as that destructor. The language standard doesn't specify this, but that's how all the implementations I'm familiar with do it.

这篇关于模板方法在哪里实例化?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆