C ++模板和对象代码实例化 [英] C++ templates and object code instantiation

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

问题描述

有了这个问题,我想更好地了解C ++模板系统如何工作这个问题

With this question I'd like to better understand how C++ templates system works with regards to this question.

据我所知,基于模板的类和函数通常放在头文件中。这是由于管理通用数据类型的技术问题,原则上哪些特性是未知的。一旦知道它们,编译器就可以生成适合所需数据类型的可执行代码。

As far as I know, template-based classes and functions are usually placed into header files. This is due to the technical issue of managing generic data types, which characterstics are unknown in principle. As soon as they are known, the compiler can generate the executable code which is suited for the required data type.

在头文件中。 h ,我们的类将被定义如下:

In the header file something.h, our class shall be defined as follows:

template <typename T>
class Something
{
    public:
        void setElement (T &elem) {
            element = elem;
        }
        T getElement () {
            return element;
        }
    private:
        T element;
};

现在让我们假设拆分源代码和类定义:

Now let's suppose to split source and class definition:

下面的类定义将写在 something.h 中:

The following class definition will be written in something.h:

template <typename T>
class Something
{
    public:
        void setElement (T &elem);
        T getElement ();
    private:
        T element;
};


$ b < $ c>:

While the following methods will be written in something.cpp:

#include "something.h"

template <typename T>
void Something<T>::setElement (T &elem)
{
    element = elem;
}

template <typename T>
T Something<T>::getElement ()
{
    return element;
}

除非我们在中声明一些特定类型的实例.cpp ,如果我们将它编译为对象文件,我们将不会在其中获取任何文本段:

Unless we declare some specific-type instances inside something.cpp, if we compile it as object file we won't obtain any text section inside it:

dacav@mithril:<tmp>$ g++ something.cpp  -c
dacav@mithril:<tmp>$ objdump -D something.o 

something.o:     file format elf64-x86-64


Disassembly of section .comment:

0000000000000000 <.comment>:
   0:   00 47 43                add    %al,0x43(%rdi)
   3:   43 3a 20                rex.XB cmp    (%r8),%spl
...
...
  20:   34 2e                   xor    $0x2e,%al
  22:   31 00                   xor    %eax,(%rax)
dacav@mithril:<tmp>$

由于Martin York显示我们可以强制编译器为一些特定的数据类型生成代码,以控制哪些类型可以使用和哪些不能。但是如果我们不想要任何限制呢?

As Martin York shows we can force the compiler to generate the code for some specific data types in order to control which types can be used and which cannot. But what if we don't want any restriction?

推荐答案

标准没有定义一个export关键字,它应该从文件中导出可实例化的(即原始形式,而不是特定类型)模板。然而,现实是,几乎没有主要的编译器支持它,并说他们永远不会支持它。因此,它从C ++ 0x中删除。

You're stuffed, in short. The Standard did define an "export" keyword, that was supposed to export instantiable (i.e., the raw form, not a specific type) templates from a file. However, the reality is that virtually no major compilers support it and said that they would never support it. Therefore, it was removed from C++0x.

这篇关于C ++模板和对象代码实例化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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