English 中文(简体)
• 如何超越C++分配班的施工方法的违约信条 附件
原标题:How to overwrite the default behavor of the construct method in the allocator class in C++ STL
  • 时间:2011-11-11 04:53:55
  •  标签:
  • c++
  • stl

你们如何超越STL中分配班的施工方法的违约行为? 下述情况似乎并不可行:

#include <list>
#include <iostream>
#include <memory>

struct MyObj {
    MyObj() {
        std::cout << "This is the constructor" << std::endl;
    }
    MyObj(const MyObj& x) {
        std::cout << "This is the copy constructor" << std::endl;
    }
};

class MyAlloc : public std::allocator <MyObj>{
public:
    void construct(pointer p, const_reference t){
        std::cout << "Construct in the allocator" << std::endl;
        new( (void*)p ) MyObj(t);
    }
};

int main(){
    MyObj x;         
    std::list <MyObj,MyAlloc> list(5,x);
} 

该方案的回报

This is the constructor
This is the copy constructor
This is the copy constructor
This is the copy constructor
This is the copy constructor
This is the copy constructor

我愿回过来。

This is the constructor
Construct in the allocator
This is the copy constructor
Construct in the allocator
This is the copy constructor
Construct in the allocator
This is the copy constructor
Construct in the allocator
This is the copy constructor
Construct in the allocator
This is the copy constructor
问题回答

欢迎分配者这一美好的世界。 我希望你们会得到你们的停留,尽管这是不可能的。

细则1:不产生于<代码>std:alplaceor。 如果你想使用自己的分配办法,那么就写上你自己的拨款人。 如果你想要“摆脱”在以下几个功能中的某些功能:定位器,则简单地创建<代码>std:allotore ,并将其职能称作无隐藏的职能。

注:衍生物实际上不会奏效。 在C++03中,分配器被允许拥有国家,而单位点计为国家。 因此,分配器可能没有虚拟功能。 正因如此,<条码> : 并没有虚拟功能。

第2条:<代码>std:list<T>never<>m>>>>>>分拨/code>。 成员:<代码>std:list是一个链接清单。 它分配nodes,其中有一个T作为成员。 它通过一些模版的魔术来这样做,它把你的器级作为参数,将其内部分辨率作为参数,并将同一模板的新分配器目标退回,但采用不同的模板参数。

It does this via a template struct member of your allocator call rebind, which has a member typedef called other that defines the new allocator type. In your case, std::list will do this:

MyAlloc::rebind<_ListInternalNodeType>::other theAllocatorIWillActuallyUse();

And that is still provided by the base class. So the type of MyAlloc::rebind<_ListInternalNodeType>::other is std::allocator<_ListInternalNodeType>. Which is the allocator type that std::list will use to actually allocate things.

你必须做比你在你的法典中做些什么。 这是为使《公约》发挥作用而需要的最低法典:

template<typename T>
class MyAlloc : public std::allocator <T>
{
public:
     typedef size_t     size_type;
     typedef ptrdiff_t  difference_type;
     typedef T*         pointer;
     typedef const T*   const_pointer;
     typedef T&         reference;
     typedef const T&   const_reference;
     typedef T          value_type;


     template<typename U>
     struct rebind
     {
       typedef MyAlloc <U> other; 
     };

     MyAlloc() {}

     template<typename U>
     MyAlloc(const MyAlloc<U>&) {}

     void construct(pointer p, const_reference t){
        std::cout << "Construct in the allocator" << std::endl;
        new( (void*)p ) MyObj(t);
     }

};

然后将其用作:

   int main(){
    MyObj x;         
    std::list <MyObj,MyAlloc<MyObj> > list(5,x);
}

产出(按照您的愿望):

This is the constructor
Construct in the allocator
This is the copy constructor
Construct in the allocator
This is the copy constructor
Construct in the allocator
This is the copy constructor
Construct in the allocator
This is the copy constructor
Construct in the allocator
This is the copy constructor

Online demo : http://www.ideone.com/QKdqm

这一最低代码的总体想法是超越<代码>rebind的定义。 基础类别中的班级模板std:allotor 定义如下:

template<typename U>
struct rebind
{
    typedef std::allocator<U> other; 
};

事实上,我们需要的是:

template<typename U>
struct rebind
{
    typedef MyAlloc<U> other; 
};

因为最终是rebind<U>:other,用于分配。

这样一来,就有必要使衍生类别(类型)的名称(如果出现的话,这些名称不能作为<代码>MyAlloc,现在就是一个类别模板)。 因此,你可以写道:

template<typename T>
class MyAlloc : public std::allocator <T>
{
    typedef std::allocator <T> base;
public:
     typedef typename base::size_type        size_type;
     typedef typename base::difference_type  difference_type;
     typedef typename base::pointer          pointer;
     typedef typename base::const_pointer    const_pointer;
     typedef typename base::reference        reference;
     typedef typename base::const_reference  const_reference;
     typedef typename base::value_type       value_type;

     //same as before
 };

结果是:





相关问题
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?

热门标签