auto_ptr解释道 [英] auto_ptr explained

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

问题描述

大家好:

花了一些时间搞清楚auto_ptr类的实现,我

决定写一篇小文章详细说明它对auto_ptr_ref的使用
代理类,用于启用临时构建和分配。我将
放在手稿上:

http://www.nd.edu/~ahenrick/auto_ptr.pdf


它以递增的方式解决问题及其解决方案,

有希望让大多数人都可以使用它而不会因为b $ b太麻烦。


让我知道这是否有用给你,

Andrew

Hello all:
After spending some time figuring out auto_ptr class'' implementation, I
decided to write a small article detailing its use of the auto_ptr_ref
proxy class to enable construction and assignment from temporaries. I
have placed the manuscript at:

http://www.nd.edu/~ahenrick/auto_ptr.pdf

It develops the problem and its solution in an incremental manner,
which will hopefully make it accessible for most people without being
too tedious.

Let me know if this was of use to you,
Andrew

推荐答案



Andrew写道:

Andrew wrote:
大家好:
花了一些时间搞清楚auto_ptr类的实现后,我决定写一篇小文章,详细介绍如何使用auto_ptr_ref
代理类来启用施工和临时任务。我已将稿件放在:

http://www.nd.edu/~ahenrick/auto_ptr.pdf

它以渐进的方式解决问题及其解决方案,
希望能够大多数人都可以使用它而不会太繁琐。
Hello all:
After spending some time figuring out auto_ptr class'' implementation, I
decided to write a small article detailing its use of the auto_ptr_ref
proxy class to enable construction and assignment from temporaries. I
have placed the manuscript at:

http://www.nd.edu/~ahenrick/auto_ptr.pdf

It develops the problem and its solution in an incremental manner,
which will hopefully make it accessible for most people without being
too tedious.




这实际上是一篇非常有趣的文章。我还没有读完所有

但是它很顺利。我已经将它保存在我的阅读文件夹中,我想要阅读的内容是......我愿意阅读它们。



This is actually a pretty interesting article. I haven''t read it all
but it was going good. I''ve saved it in my read folder where things I
want to read go....and yes, I usually read them.


Andrew写道:
大家好:
花了一些时间搞清楚auto_ptr类的实现后,我决定写一篇详细介绍其用法的小文章auto_ptr_ref
代理类,用于启用临时构建和分配。我已将稿件放在:

http://www.nd.edu/~ahenrick/auto_ptr.pdf

它以渐进的方式解决问题及其解决方案,
希望能够大多数人都可以使用它而不会太繁琐。

请告诉我这是否对您有用,
Andrew
Hello all:
After spending some time figuring out auto_ptr class'' implementation, I
decided to write a small article detailing its use of the auto_ptr_ref
proxy class to enable construction and assignment from temporaries. I
have placed the manuscript at:

http://www.nd.edu/~ahenrick/auto_ptr.pdf

It develops the problem and its solution in an incremental manner,
which will hopefully make it accessible for most people without being
too tedious.

Let me know if this was of use to you,
Andrew




1

==

"围绕自动ptr类的困难主要是由于

意味着

const限定符


std :: auto_ptr的主要困难是所有权转移,而不是

const

限定符" ;.


" //用于整数的auto_ptr专业化

int * dumb_ptr = new int(0);

auto_ptr i = new int(0); //我指向* dumb_ptr

auto_ptr j(i); // j指向* dumb_ptr,但我指向0"


当您举例时,请尝试使它们有效。对于小例子

(这个可能

资格),你可以省略main(),但你一定要删除

dump_ptr。而且我不认为
认为dump_ptr会为你的例子添加任何内容。


嗯,对于初学者来说,复制构造函数的默认声明是

auto_ptr(const auto_ptr& rhs);"


这不是默认声明,它是建议的

大多数用途。


从语言的角度来看,const auto的非法复制构建

ptr是

比合法复制结构更好的支持

来自
非const自动ptr。


这没有任何意义。你可以这么说,因为C ++禁止将一个

非const

引用绑定到rvalue,副本中有一个非const参数

构造函数限制

使用它。


1.1

===

"这是为了防止程序员无意间试图操作临时结束后无效的临时数据

声明:


double& dr = std :: sin(2.0); //标准C ++中的错误

dr + 2;"


这部分错误且限制性太强。你可以说这是为了防止

程序员修改一个右值(不是一个临时的)它什么时候会让b $ b毫无意义
/>
来修改它。


关于引用的规则不是关于临时的,而是关于rvalues。

rvalues是

temporaries,但并非所有临时都是rvalues。例如,

异常是临时的,并且可能绑定到非const引用;它不是一个价值
rvalue。你应该在整个文档中修改它。


"因此,下一个语句试图引用一个变量

无效;


这是非法的,而不是无效的 (这没什么意思)。


2

==

您的示例格式错误。添加更多空格。


" 1。这是针对整数而非模板的专业化。


单词specialization通常用于模板,所以

我会说

这个类显然使用int *作为指针类型。


" 3.我已将消息添加到cerr以跟踪程序执行。


不要。改为使用std :: clog。


" 4。函数定义包含在类定义中(参见第2页

脚注)。"


实际情况通常如此,因为std :: auto_ptr是一个模板。


" 5。我有更改[...]


更改,而不是更改。


2.1

===

"这是作为临时对象返回的,地址以

ab0结尾。


删除a。


3

=

首先,在考虑运营商=时,我们注意到,因为赋值

改变了两个参数的所有权

,禁止任何一个参数是一个const auto

ptr"


operator =只有一个参数(没有指定如何传递这个

指针)。更准确地说,禁止使用运算符=

调用的参数或对象是const。


" auto_ptr& operator =(auto_ptr& rhs)

{

reset(rhs.release());

return * this;

std :: cerr<<这个<< auto_ptr从分配 << & rhs<< std :: endl;

}


auto_ptr& operator =(const auto_ptr_ref& rhs)throw()

{

reset(rhs.yp);

return * this;

std :: cerr<<这个<< " auto_ptr从分配 << & rhs

<< " :auto_ptr_ref - > auto_ptr的" << std :: endl;

}"


两个std :: cerr语句都无法访问。如果

你已经正确格式化了代码,你会看到它。


" auto_ptr j();

j = source();"


j是一个在这里返回auto_ptr的函数。


4

==

到目前为止,讨论一直独立于模板

元编程:"


重新说明。


问题及其解决方案都是直接面向对象的

C ++。


模板化代码不是直接面向对象的C ++?

相当不错的东西,粗体。

Jonathan



1
==
"The diffculties surrounding the auto ptr class arise primarily due to
meaning of
the const qualifier"

The main difficulty with std::auto_ptr is with ownership transfer, not
the "const
qualifier".

"// auto_ptr specialization for ints
int* dumb_ptr = new int(0);
auto_ptr i = new int(0); // i points to *dumb_ptr
auto_ptr j(i); // j points to *dumb_ptr but i points to 0"

When you give examples, try to make them valid. For small examples
(this one may
qualify), you may omit main(), but you should definitely delete
dump_ptr. And I don''t
think dump_ptr adds anything to your example.

"Well, for starters, the default declaration of the copy constructor is
auto_ptr(const auto_ptr& rhs);"

This is not the "default" declaration, it is the recommended one for
most uses.

"From a language standpoint, illegal copy construction of a const auto
ptr is
something which is better supported than the legal copy construction
from a
non-const auto ptr."

This makes no sense. You could say that because C++ prohibits binding a
non-const
reference to an rvalue, having a non-const parameter in the copy
constructor limits
its use.

1.1
===
"This is to prevent programmers from unintentionally trying to
manipulate temporaries which are invalid after the end of the scoping
statement:

double& dr = std::sin(2.0); // ERROR in standard C++
dr + 2;"

This is partly wrong and too restrictive. You could say that this is to
prevent
programmers from modifying an rvalue (not a temporary) when it would
make no sense
to modify it.

The rule about references is not about temporaries, but rvalues.
rvalues are
temporaries, but not all temporaries are rvalues. For example, an
exception is a
temporary and may be bound to a non-const reference; it is not an
rvalue. You should
modify this in the whole document.

"Thus, the next statement tries to reference a variable which is
invalid;"

which is illegal, not "invalid" (which means nothing).

2
==
You examples are badly formatted. Add more spaces.

"1. This is a specialization for ints, not a template."

The word "specialization" is usually used in relation to a template, so
I would say
that this classes explicitly uses an int* as the pointee type instead.

" 3. I have added messaging to cerr to follow program execution."

Don''t. Use std::clog instead.

"4. Function definitions are included in the class definition (see 2nd
footnote)."

This is usually the case actually, because std::auto_ptr is a template.

"5. I have change [..]

"changed", not "change".

2.1
===
"This is returned as a the temporary object with address ending in
ab0."

Remove the "a".

3
=
"First, in considering operator=, we note that, because assignment
changes ownership
of both arguments, it is forbidden that either argument is a const auto
ptr"

operator= only has one argument (it is not specified how the this
pointer is passed). It would be more accurate to say that it is
forbidden that either the argument or the object on which operator= is
called is const.

"auto_ptr& operator= (auto_ptr& rhs)
{
reset(rhs.release());
return *this;
std::cerr << this << "auto_ptr assigned from " << &rhs << std::endl;
}

auto_ptr& operator= (const auto_ptr_ref& rhs) throw()
{
reset(rhs.yp);
return *this;
std::cerr << this << " auto_ptr assigned from " << &rhs
<< " : auto_ptr_ref -> auto_ptr" << std::endl;
}"

Both std::cerr statements are unreachable. You would have seen it if
you had formatted
the code correctly.

"auto_ptr j();
j = source();"

j is a function returning an auto_ptr here.

4
==
"Thus far the discussion has been independent of template
meta-programming:"

Rephrase that.

"both the problem and its solution are straight forward object-oriented
C++."

Templated code is not "straight forward object-oriented C++"?
Pretty good stuff, bravo.
Jonathan


Jonathan Mcdougall写道:
Jonathan Mcdougall wrote:
"围绕自动ptr类的困难主要是由于const的含义而产生的限定符

std :: auto_ptr的主要困难是所有权转移,而不是const限定符&qu OT ;.


点数;但是这篇文章并不直接关于所有权转移。

这是关于使用auto_ptr_ref代理类来防止复制

构造或复制const auto_ptr的赋值。

" //用于整数的auto_ptr专业化
int * dumb_ptr = ...

当你举例时,试着让它们有效....


谢谢 - 更正了

嗯,对于初学者来说,复制构造函数的默认声明是
auto_ptr(const auto_ptr& rhs);"

这不是默认。声明,它是大多数用途的推荐。


我提供了几个参考资料。

从语言的角度来看,非法复制建筑......

毫无意义...


谢谢,我已经改变了句子

这是为了防止程序员无意中试图操纵临时工在范围结束后声明无效

双& dr = std :: sin(2.0); //标准C ++中的错误
dr + 2;

这部分错误且限制性太强。你可以说这是为了防止程序员修改rvalue ...


我将不会在这篇文章中引入左值/右值条款

因为这些术语本身似乎没有被C ++社区统一理解

(例如只搜索这个新闻组)。

临时对象,在另一方面手,很容易理解。

关于引用的规则不是关于临时性的,而是rvalues ...
你应该在整个文档中修改它。


我不明白你的评论。这个概念几乎直接来自Stroustrup的书,正如pg所引用的那样。 98:引用

变量并引用常量是有区别的,因为

在变量的情况下引入临时值很高

容易出错; ...


如果你能用这个

语句指出什么是错误或限制太多,我会修复它。目前,它可能不完整,但它肯定是正确的。

"因此,下一个语句试图引用一个变量
无效;

这是非法的,而不是无效的 (这意味着什么)。


谢谢,现在措辞更好...

您的示例格式错误。添加更多空格。


恕我直言,他们没事。如果您可以提出具体建议,那将是

帮助。

专业化一词通常用于模板,
所以我会说这个类明确地使用int *作为指针
类型。


这是真的,但auto_ptr通常作为模板实现。我认为这意思很清楚。

" 3.我已将消息添加到cerr以跟踪程序执行。

不要。请改用std :: clog。


我认为std :: cerr对此更好,因为它没有被缓冲。

"这是作为临时对象返回的地址以ab0结尾。

删除a。


我将继续使用3个字母/数字来指代地址。阻止

说到地址90.

operator =只有一个参数...


谢谢,看看更正。

两个std :: cerr语句都无法访问....


Opps ...这令人尴尬。谢谢 - 更正了

" auto_ptr j();

j是一个在这里返回auto_ptr的函数。


固定

到目前为止讨论独立于模板
元编程:

换句话说。


OK

模板化代码不是直接的面向对象的C ++?


这就是句子的意思。

相当不错的东西,勇敢。

Jonathan
"The diffculties surrounding the auto ptr class arise primarily due to
meaning of the const qualifier"

The main difficulty with std::auto_ptr is with ownership transfer, not
the "const qualifier".
Point taken; but this article is not directly about ownership transfer.
It is about the use of auto_ptr_ref proxy class to prevent copy
construction or copy assignment of a const auto_ptr.
"// auto_ptr specialization for ints
int* dumb_ptr = ...

When you give examples, try to make them valid....
Thanks -- corrected
"Well, for starters, the default declaration of the copy constructor is
auto_ptr(const auto_ptr& rhs);"

This is not the "default" declaration, it is the recommended one for
most uses.
I have provided a couple of references.
"From a language standpoint, illegal copy construction...

This makes no sense...
Thanks, I have changed the sentence
"This is to prevent programmers from unintentionally trying to
manipulate temporaries which are invalid after the end of the scoping
statement:

double& dr = std::sin(2.0); // ERROR in standard C++
dr + 2;"

This is partly wrong and too restrictive. You could say that this is to
prevent programmers from modifying an rvalue...
I will refrain from introducing lvalue/rvalue terms in this article
because the terms themselves do not appear to be uniformly understood
by the C++ community (just search this newsgroup for instance).
Temporary objects, are, on the other hand, trivial to understand.
The rule about references is not about temporaries, but rvalues...
You should modify this in the whole document.
I do not understand your comment here. This concept is taken almost
straight out of Stroustrup''s book, as cited on pg. 98: "References to
variables and referenced to constants are distinguished because the
introduction of a temporary in the case of variable is highly
error-prone;..."

If you can point out what is wrong or too restrictive with this
statement, I will fix it. As it stands, it may not be complete, but it
certainly is correct.
"Thus, the next statement tries to reference a variable which is
invalid;"

which is illegal, not "invalid" (which means nothing).
Thanks, better worded now...
You examples are badly formatted. Add more spaces.
IMHO, they are fine. If you can make a specific suggestion, that would
help.
The word "specialization" is usually used in relation to a template,
so I would say that this classes explicitly uses an int* as the pointee
type instead.
That is true, but auto_ptr is normally implemented as a template. I
think the meaning is clear.
" 3. I have added messaging to cerr to follow program execution."

Don''t. Use std::clog instead.
I think that std::cerr is better for this, since it isn''t buffered.
"This is returned as a the temporary object with address ending in ab0."

Remove the "a".
I''ll keep referring to the addresses using 3 letters/numbers. Prevents
speaking of address 90.
operator= only has one argument...
Thanks, see correction.
Both std::cerr statements are unreachable....
Opps... that''s embarrassing. Thanks -- corrected
"auto_ptr j();

j is a function returning an auto_ptr here.
Fixed
"Thus far the discussion has been independent of template
meta-programming:"

Rephrase that.
OK
Templated code is not "straight forward object-oriented C++"?
That is the point of the sentence.
Pretty good stuff, bravo.

Jonathan



谢谢,Jonathan,阅读我的文章并花时间发送

我更正。我认为现在更清楚了。


Andrew



Thanks, Jonathan, for reading my article and taking the time to send
me corrections. I think that it is much more clear now.

Andrew


这篇关于auto_ptr解释道的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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