删除对应于展示位置的就地? [英] delete in-place corresponding to placement new?

查看:64
本文介绍了删除对应于展示位置的就地?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚构建了一个类,它提供了最有用的std :: vector

功能子集,供缺乏模板功能的编译器使用。

http://home.att.net/~olcott/std_vect.html


通过这个新闻组的建议,除了一个方面外,我能够完全复制std :: vector的接口

。我需要的是一种方式

来调用数组元素上的析构函数而不取消分配这个数组的

内存。这将对应于新的位置,在没有分配的情况下构建

。想到的最明显的方法是仅仅显示
显式调用析构函数。大约

1990编译器不提供此功能。有没有人有什么好主意?


I have just built a class that provides the most useful subset of std::vector
functionality for use by compilers that lack template capability.

http://home.att.net/~olcott/std_vect.html

Through suggestions from this news group I was able to exactly duplicate
the interface of std::vector, except for one aspect. What I need is a way
to invoke the destructor on elements of an array without de-allocating the
memory of this array. This would correspond to placement new, constructing
without allocating. The most obvious way that comes to mind is to merely
explicitly invoke the destructor. This capability is not available on a circa
1990 compiler. Does anyone have any good ideas?


推荐答案



" Peter Olcott" <醇**** @ worldnet.att.net>在消息中写道

新闻:OF ********************* @ bgtnsc05-news.ops.worldnet.att.net ...

"Peter Olcott" <ol****@worldnet.att.net> wrote in message
news:OF*********************@bgtnsc05-news.ops.worldnet.att.net...
我刚刚构建了一个类,它提供了
std :: vector功能最有用的子集,供缺乏模板功能的编译器使用。

http://home.att.net/~olcott/std_vect.html

通过这个新闻组的建议我能够完全
复制std :: vector的接口,除了一个方面。


所以你有新工作的位置?恭喜!

我需要的是一种在数组元素上调用析构函数的方法,而不需要
来分配这个数组的内存。这将对应于新分配,
构建而不分配。想到的最明显的方法是
只是显式调用析构函数。


这就是这样做的方式。

这个功能在大约1990年的编译器上不可用。有没有人有什么好主意?
I have just built a class that provides the most useful subset of std::vector functionality for use by compilers that lack template capability.

http://home.att.net/~olcott/std_vect.html

Through suggestions from this news group I was able to exactly duplicate the interface of std::vector, except for one aspect.
So you got placement new to work? Congratulations!
What I need is a way
to invoke the destructor on elements of an array without de-allocating the memory of this array. This would correspond to placement new, constructing without allocating. The most obvious way that comes to mind is to merely explicitly invoke the destructor.
That''s the way to do it.
This capability is not available on a circa
1990 compiler. Does anyone have any good ideas?




如果你不介意打扰,你可以要求每个

用户定义的类型提供销毁功能。否则,我不知道。


你使用什么语法进行析构函数调用?


Jonathan



If you don''t mind being intrusive, you can require that each
user-defined type provide a destroy function. Otherwise, I don''t know.

What syntax are you using for the destructor invocation?

Jonathan


Peter Olcott在

新闻中写道:OF ********************* @ bgtnsc05-news.ops.worldnet.att.net:
Peter Olcott wrote in
news:OF*********************@bgtnsc05-news.ops.worldnet.att.net:
我刚刚构建了一个类,它提供了
std :: vector功能最有用的子集供编译器使用缺乏模板
能力。

http://home.att.net/~olcott/std_vect.html

通过这个新闻组的建议,我能够完全复制std :: ::矢量,除了一个方面。我需要的是一种在数组元素上调用析构函数的方法,而无需取消分配此数组的内存。这将对应于
placement new,在不分配的情况下构建。想到的最明显的方法是仅显式调用析构函数。大约在1990年的编译器上没有这种功能。有没有人有什么好主意?
I have just built a class that provides the most useful subset of
std::vector functionality for use by compilers that lack template
capability.

http://home.att.net/~olcott/std_vect.html

Through suggestions from this news group I was able to exactly
duplicate the interface of std::vector, except for one aspect. What I
need is a way to invoke the destructor on elements of an array without
de-allocating the memory of this array. This would correspond to
placement new, constructing without allocating. The most obvious way
that comes to mind is to merely explicitly invoke the destructor. This
capability is not available on a circa 1990 compiler. Does anyone have
any good ideas?




假设你做不到:


template< ; typename T>

inline void destroy(T * t)

{

t-> ~T();

}


然后你可以试试:


#define OFFSETOFF(Class,Member)\

(((char *)&((Class *)0) - > Member) - ((char *)0))


template< class T>

struct cheat_detor

{

T item;


inline void operator delete (void *){}


static void destroy(T * ptr)

{

删除(

(cheat_detor< T> *)

(((char *)ptr) - OFFSETOFF(cheat_detor< T>,item))

); < br $>
}

};


模板< class T>

inline void destroy(T * ptr)

{

cheat_detor< T> :: destroy(ptr);

}


以上是非标准代码,事实上g ++会发出警告

如果T是非POD类型说:


struct X

{

X(){}

~X(){}

};


Rob。

-
http://www.victim-prime.dsl.pipex.com/


>所以你有新工作的位置?恭喜!

很多人建议放置新的,tom_usenet不是

只告诉我如何让它在现代编译器上工作,

但是也提供了所需的代码使它在我的

古董编译器上运行。
> So you got placement new to work? Congratulations!
Many people suggested placement new, tom_usenet not
only showed me how to make it work on modern compilers,
but also provided the required code to make it work on my
antique compiler.
如果你不介意打扰,你可以要求每个
用户定义的类型提供销毁功能。否则,我不知道。
这就是我开始工作的方式。在放置new之前,我还必须有两个构造函数:

构造函数:Construct()和Construct(const&)

您使用什么语法进行析构函数调用?
当前所需的成员函数仅仅是:

Destruct(); //这会调用用户定义的析构函数的主体。

最简单的方法是在Destruct()中提供析构函数的主体

,然后让~UserType(){Destruct(); Jonathan
If you don''t mind being intrusive, you can require that each
user-defined type provide a destroy function. Otherwise, I don''t know. This is how I got it to work. Before placement new, I also had to have
two Construct functions: Construct() and Construct(const &)
What syntax are you using for the destructor invocation? The currently required member function is merely:
Destruct(); // This invokes the body of the user defined destructor.
The easiest way to do this is to provide the body of the destructor
within Destruct(), and then have ~UserType() { Destruct(); };
Jonathan



这篇关于删除对应于展示位置的就地?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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