English 中文(简体)
C++ 模板和物体代码即时
原标题:C++ templates and object code instantiation

问题 我愿更好地了解C++模板系统在方面的工作。 问题

我知道,基于模板的班级和职能通常被放在头版档案中。 这是因为管理通用数据类型的技术问题,而这种特性在原则上不为人所知。 一旦知道,汇编者可产生适合所需数据类型的可执行守则。

在标题档案something.h中,我们的类别定义如下:

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

如今,允许将来源和类别定义分开:

以下类别定义将载于<编码>随附/code>:

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

虽然以下方法将在<编码>中填写。

#include "something.h"

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

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

除非我们宣布在<条形码>中的某些特定类型的事例,否则,如果我们将其编成目标文件,我们就没有获得其中的任何文字部分:

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>$

如马丁纽约所示:。 我们可以迫使汇编者为某些特定数据类型制定守则,以便控制哪些类型可以使用,哪些类型无法使用。 但是,如果我们不想要限制,什么呢?

最佳回答

简言之,你 st。 《标准》的确界定了“出口”关键词,该词本应从档案中出口可即时(即原形式,而不是特定类型)模板。 然而,现实是,实际上没有任何主要汇编者支持它,并且说他们永远不会支持它。 因此,从C++0x中删除。

问题回答

如果你想能够立即提出一个任意类型的模板,那么简单地把瞬时所需的一切放在标题档案中。 这意味着将职能机构(需要随时查询)放在标题档案中,而不是放在文件上。

如果你想要在单独档案中作出声明和定义,只是为了改进源代码的结构,你可以采用这样的办法:

something.h

// Just the declaration

template<typename T>
class Something {
  void foo();
};

// include the header file with the definitons
#include "something.impl.h"

something.impl.h

// Put definitions here

template<typename T>
void Something<T>::foo() {
}




相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?

热门标签