分配器要求 [英] allocator requirements

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

问题描述

如果在STL容器中指定分配器,则需要分配器分配正确类型的对象,或者您可以假设容器将是

将分配器重新绑定到正确的类型?


例如,我尝试使用以下代码,该代码使用了错误的分配器,并且

被略微勉强找到它汇编了我试过的三个编译器




#include< vector>

#include< memory>


int main()

{

std :: vector< int,std :: allocator< double> > vec;

vec.push_back(1);

}


john

If you specify an allocator in an STL container is it a requirement that
the allocator allocates object of the right type, or can you assume that
the container will rebind the allocator to the correct type?

For example I tried the following code which uses a ''wrong'' allocator and
was slightly surrised to find it compiles on the three compilers I tried
it on

#include <vector>
#include <memory>

int main()
{
std::vector<int, std::allocator<double> > vec;
vec.push_back(1);
}

john

推荐答案

John Harrison写道:
John Harrison wrote:
如果在STL容器中指定分配器,则需要分配器分配正确类型的对象,或者你能否假设容器会将分配器重新绑定到正确的类型?


容器很可能使用''construct''成员

函数,它接受任何类型的内存(用于放置

new)和double *足以构造int值

in。你可能会得到一些过多的内存使用,但

not任何严重的问题。反向尝试,有一个

的向量,并使用allocator< char>只是为了踢...


至于''重新绑定'',我在容器上的章节

中找不到任何引用。假设你可以依靠...我不会。

例如我尝试了下面的代码,它使用了一个''错误''分配器
并且稍微有点罢了找到它编译我编写的三个编译器

#include< vector>
#include< memory>

int main()
{
std :: vector< int,std :: allocator< double> > vec;
vec.push_back(1);
}
If you specify an allocator in an STL container is it a requirement
that the allocator allocates object of the right type, or can you
assume that the container will rebind the allocator to the correct type?
It''s quite possible that the container uses ''construct'' member
function, which accepts the memory of any type (for placement
new) and double* is good enough for int values to be constructed
in. You probably get some excessive memory use that way, but
not any serious problem. Try it in reverse, have a vector of
double and use allocator<char> just for kicks...

As to ''rebind'', I can''t find any reference to it in the chapter
on containers. Assume you can, rely upon... I wouldn''t.

For example I tried the following code which uses a ''wrong'' allocator
and was slightly surrised to find it compiles on the three compilers I
tried it on

#include <vector>
#include <memory>

int main()
{
std::vector<int, std::allocator<double> > vec;
vec.push_back(1);
}




V



V


2004年7月9日星期五,格林尼治标准时间20:20:34,Victor Bazarov< v。******** @ comAcast.net>

写道:
On Fri, 09 Jul 2004 20:20:34 GMT, Victor Bazarov <v.********@comAcast.net>
wrote:
John Harrison写道:
John Harrison wrote:
如果你在STL容器中指定一个分配器是一个要求
分配器分配正确类型的对象,或者你可以
假设容器会将分配器重新绑定到正确的
类型?
If you specify an allocator in an STL container is it a requirement
that the allocator allocates object of the right type, or can you
assume that the container will rebind the allocator to the correct
type?



容器很可能使用''construct''成员
函数,它接受任何类型的内存(用于放置
new)和double *足以构造
in。你可能会以这种方式获得一些过多的内存使用,但是
没有任何严重的问题。反向尝试,有一个
的向量,并使用allocator< char>只是为了踢...

至于''重新绑定'',我在集装箱的章节中找不到任何参考。假设你可以依靠...我不会。



It''s quite possible that the container uses ''construct'' member
function, which accepts the memory of any type (for placement
new) and double* is good enough for int values to be constructed
in. You probably get some excessive memory use that way, but
not any serious problem. Try it in reverse, have a vector of
double and use allocator<char> just for kicks...

As to ''rebind'', I can''t find any reference to it in the chapter
on containers. Assume you can, rely upon... I wouldn''t.

例如我尝试了下面的代码,它使用了错误的分配器
我发现它在三个编译器上编译了我在
#include< vector>
#include< memory>
int main()
{
std :: vector< int,std :: allocator< double> > vec;
vec.push_back(1);
}
For example I tried the following code which uses a ''wrong'' allocator
and was slightly surrised to find it compiles on the three compilers I
tried it on
#include <vector>
#include <memory>
int main()
{
std::vector<int, std::allocator<double> > vec;
vec.push_back(1);
}



V



V




作为证据我尝试了以下


#include< iostream>

#include< vector>

#include< memory>


typedef std :: vector< int,std :: allocator< double> > :: allocator_type X;


void test(std :: allocator< double> const&)

{

std :: cout<< double \\\
;

}


void test(std :: allocator< int> const&)

{

std :: cout<< int \ n;

}


int main()

{

test(X());

}


你期望输出是什么,''int''或''double''?两个编译器

我在输出上试了它''int''。这意味着STL

的那些版本将分配器重新绑定为int分配器。


john



As a corrolary I tried the following

#include <iostream>
#include <vector>
#include <memory>

typedef std::vector<int, std::allocator<double> >::allocator_type X;

void test(std::allocator<double> const&)
{
std::cout << "double\n";
}

void test(std::allocator<int> const&)
{
std::cout << "int\n";
}

int main()
{
test(X());
}

What would you expect the output to be, ''int'' or ''double''? Both compilers
I tried it on the output was ''int''. Which means those versions of the STL
are rebinding the allocator to be an int allocator.

john




" John Harrison" <乔************* @ hotmail.com>在消息中写道

news:opsavy7sdb212331 @ andronicus ...

"John Harrison" <jo*************@hotmail.com> wrote in message
news:opsavy7sdb212331@andronicus...
如果在STL容器中指定分配器,那么分配器分配对象的需求是
正确的类型,或者你可以假设
容器将分配器重新绑定到正确的类型?
If you specify an allocator in an STL container is it a requirement that the allocator allocates object of the right type, or can you assume that the container will rebind the allocator to the correct type?




我认为这是由23.1 [lib。 container.requirements] para。

8:

复制本子句中定义的所有容器类型的构造函数

一个allocator参数从他们的

各自的第一个参数。这些

容器类型的所有其他构造函数都使用Allocator&参数

(20.1.5),一个分配器,其值类型与容器的

值类型相同。


Jonathan



I think this is addressed by 23.1 [lib.container.requirements] para.
8:

Copy constructors for all container types defined in this clause copy
an allocator argument from their
respective first parameters. All other constructors for these
container types take an Allocator& argument
(20.1.5), an allocator whose value type is the same as the container''s
value type.

Jonathan


这篇关于分配器要求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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