关于C ++ Allocatars的问题 [英] Question about C++ Allocatars

查看:92
本文介绍了关于C ++ Allocatars的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对C ++专业人员和C ++标准化会员的成员有所了解。


我知道C ++标准版要求分配器在每个STL容器中都是模板化参数。 br />
除此之外,还可以将对allocatior的引用传递给每个STL容器。这提出了以下问题:

1.使用不同分配器模板化的容器被认为是不同的类型,因此不能相互分配。这应该被视为缺点还是功能?

2.很难实现池/共享内存分配逻辑,因为任何STL容器的每个实例都有自己的分配器副本。


我不完全确定第一个是否应该被认定为缺点或功能。假设不是STL当前实现分配器的方式,而是从公共基类继承自定义分配器,并且每个容器都有一个指向该基类的指针。在这种情况下,我们可以轻松地将使用不同分配器创建的容器分配给彼此。但是,我不知道我们是否应该考虑分配器(或指向分配器的指针)部分容器数据。也就是说,如果分配器被实现为指向基类的指针,那么当一个容器被分配给另一个容器时,我们会复制它们吗?可能不是,因为分配方案是在初始化时给容器的东西而且改变它没有多大意义?如果我们不考虑分配器是容器数据的一部分,那么在STL中我们不能将容器分配给彼此,如果它们用不同的分配器模板化的话。


到第二个item关注的是,如果将allocators实现为基类的指针,那么在容器之间共享复杂的分配方案(例如池或共享内存分配)会更容易。

目前每个容器都有自己的容器alolocator的副本。


我想听听你对此事的意见(分配器作为模板参数与继承自公共基类的分配器,指向容器可以共享的指针)。 br />
在此先感谢,

$ div $

I have a quastion for the C++ professionals and members of the C++ standartization commetee.

As i know C++ standart requires an allocator to be a templated parameter in every STL container.
Besides that one can pass reference to allocatior into every STL container. This raises the following questions:
1. Containers that were templatized with different allocators are considered different types and thus cannot be assigned to each other. Should this be considered a drawback or a feature?
2. It is hard to implement pool/shared memory allocation logic since every instance of any STL container will has its own copy of an allocator.

I am not entirely sure whether the first one should be qualified as a drawback or as a feature. Suppose that instead of the way STL implements allocators currently, custom allocators would be inherited from a common base class and every container would have a pointer to that base class. In this case we could easily assign containers created with different allocators to each other. Hoever, I dont know whether we should consider allocators(or pointers to allocators) part of the container data. That is, if allocators were implemented as pointers to base class, would we copy them when one container is assigned to another? Probably not, since the allocation scheme is something given to container at the initialization time and changing that does not make much sense? If we dont consider allocators part of container data, then it is a drawback that in STL we cannot assign containers to each other if they were templatized with different allocators.

As far as the second item is concerned, if allocators were implemented as pointers to base class, it would be easier to share the complicated allocation schemes (such as pool or shared memory allocation) among containers.
Currently each container gets its own copy of alolocator.

Please i would like to hear you opinion regarding this matter (allocators as template parameters vs. allocators inherited from common base class, pointer to which containers can share).
Thanks in advance,

OM

推荐答案

" ; regisser" < OM ****** @ hotmail.com>在消息中写道

news:X _ ***************** @ fe03.private.usenetserver .com ...
"regisser" <om******@hotmail.com> wrote in message
news:X_*****************@fe03.private.usenetserver .com...
我对C ++专业人员和C ++
标准化会员的成员有所了解。


我不是标准委员会的成员,但我想我是C ++

professional。

我知道C ++标准版需要在每个STL容器中将分配器作为模板化参数
。除此之外,可以将对allocatior的引用传递到每个STL
容器中。这提出了以下问题:1。使用不同分配器模板化的容器是
,它们被认为是不同的类型,因此不能相互分配。应该

这被视为缺点还是功能?


将一个STL容器复制到另一个容器非常容易 - 甚至是一个列表
例如
a vector。如果你想看=符号,你可以写两个特定类型的

自己的运算符=。因此,这一点对我来说似乎不是一个问题。

2.由于每个
I have a quastion for the C++ professionals and members of the C++ standartization commetee.

I''m not a member of the standards committee but I suppose I am a C++
professional.

As i know C++ standart requires an allocator to be a templated parameter in every STL container. Besides that one can pass reference to allocatior into every STL container. This raises the following questions: 1. Containers that were templatized with different allocators are considered different types and thus cannot be assigned to each other. Should
this be considered a drawback or a feature?

It is extremely easy to copy one STL container to another -- even a list to
a vector, for instance. If you like to see the = sign, you can write your
own operator = for two specific types. Hence this point doesn''t seem like a
problem to me.
2. It is hard to implement pool/shared memory allocation logic since every

<很难实现池/共享内存分配逻辑任何STL容器的
实例都有自己的分配器副本。


我不太了解编写分配器来评论这个问题。 />

[snip]

-

Cy
http://home.rochester.rr.com/cyhome/


编写单独的函数,您将遍历整个

容器,逐个复制其元素,如您所建议的那样极其缓慢。尝试使用==

运算符或复制构造函数将一个向量复制到另一个向量中,您将看到它比通过迭代复制元素要快数百个b / b
倍他们。因此这是一个问题。


OM


Cy Edmunds写道:
Writing separate function where you will iterate through entire
container to copy its elements one by one, as you suggested is extremely
slow. Try as an example to copy one vector into another using ==
operator or copy constructor and you will see that it works hundreds of
times faster then iterating throug elements to copy them. It IS
therefore an issue.

OM

Cy Edmunds wrote:
regisser < OM ****** @ hotmail.com>在消息中写道
新闻:X _ ***************** @ fe03.private.usenetserver .com ...
"regisser" <om******@hotmail.com> wrote in message
news:X_*****************@fe03.private.usenetserver .com...
我对C ++专业人员和C ++成员
I have a quastion for the C++ professionals and members of the C++



标准化会议有所了解。

我不是标准委员会的成员,但我想我是C ++专业人士。



standartization commetee.

I''m not a member of the standards committee but I suppose I am a C++
professional.

我知道C ++标准版要求分配器是模板参数
As i know C++ standart requires an allocator to be a templated parameter


在每个STL容器中。



in every STL container.

除了可以将allocatior的引用传递到每个STL
Besides that one can pass reference to allocatior into every STL



容器中。这提出了以下问题:



container. This raises the following questions:

1。使用不同分配器进行模板化的容器
1. Containers that were templatized with different allocators are



被视为不同类型,因此无法相互分配。这应该被认为是一个缺点还是一个功能?

将一个STL容器复制到另一个STL容器非常容易 - 例如,甚至是一个列表。如果你想看=符号,你可以写两个特定类型的
自己的运算符=。因此,这一点对我来说似乎不是一个问题。



considered different types and thus cannot be assigned to each other. Should
this be considered a drawback or a feature?

It is extremely easy to copy one STL container to another -- even a list to
a vector, for instance. If you like to see the = sign, you can write your
own operator = for two specific types. Hence this point doesn''t seem like a
problem to me.

2。很难实现池/共享内存分配逻辑,因为任何STL容器的每个
2. It is hard to implement pool/shared memory allocation logic since every


实例都有自己的分配器副本。

我不知道我不太了解编写分配器以对此发表评论。

[snip]



instance of any STL container will has its own copy of an allocator.

I don''t know enough about writing allocators to comment on this.

[snip]



错误:使用运算符=


OM写道:
error: using operator =

OM wrote:
编写单独的函数,您将迭代整个
容器逐个复制其元素,如你所说的那样非常慢。尝试使用==
运算符或复制构造函数将一个向量复制到另一个向量中,您将看到它比通过迭代元素复制它们的速度快数百倍。因此,这是一个问题。

OM Ed / /
Cy Edmunds写道:
Writing separate function where you will iterate through entire
container to copy its elements one by one, as you suggested is extremely
slow. Try as an example to copy one vector into another using ==
operator or copy constructor and you will see that it works hundreds of
times faster then iterating throug elements to copy them. It IS
therefore an issue.

OM

Cy Edmunds wrote:
" regisser" < OM ****** @ hotmail.com>在消息中写道
新闻:X _ ***************** @ fe03.private.usenetserver .com ...
"regisser" <om******@hotmail.com> wrote in message
news:X_*****************@fe03.private.usenetserver .com...
我对C ++专业人员和C ++成员
I have a quastion for the C++ professionals and members of the C++



标准化会员有一个假设。

我不是标准委员会的成员,但我想我是C ++专业人士。


standartization commetee.

I''m not a member of the standards committee but I suppose I am a C++
professional.

我知道C ++标准版要求分配器是模板化参数
As i know C++ standart requires an allocator to be a templated parameter



。在每个STL容器中。


in every STL container.

除了可以将allocatior的引用传递给每个STL
Besides that one can pass reference to allocatior into every STL



容器。这提出了以下问题:


container. This raises the following questions:

1.使用不同分配器模板化的容器是
1. Containers that were templatized with different allocators are



被认为是不同的类型,因此不能分配给每个其他。
这应该被认为是一个缺点还是一个功能?

将一个STL容器复制到另一个容器非常容易 - 甚至是列表<例如,一个矢量。如果你想看=符号,你可以写两个特定类型的
自己的运算符=。因此,这一点对我来说似乎不是一个问题。


considered different types and thus cannot be assigned to each other.
Should
this be considered a drawback or a feature?

It is extremely easy to copy one STL container to another -- even a
list to
a vector, for instance. If you like to see the = sign, you can write your
own operator = for two specific types. Hence this point doesn''t seem
like a
problem to me.

2.很难实现池/共享内存分配逻辑自
每个
2. It is hard to implement pool/shared memory allocation logic since
every


任何STL容器的
实例都有自己的分配器副本。

我对写作不够了解分配者对此发表评论。

[snip]


instance of any STL container will has its own copy of an allocator.

I don''t know enough about writing allocators to comment on this.

[snip]




这篇关于关于C ++ Allocatars的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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