auto_ptr_ref [英] auto_ptr_ref
问题描述
Hello All,
我的问题是关于自动指针的实现 - 如何
类auto_ptr_ref有效吗?
例如:假设我有:
auto_ptr< int> foo();
int bar()
{
auto_ptr< int *> k = foo();
}
我在这里看到的确切调用顺序是(假设没有优化)
-
1)foo返回auto_ptr< int>按值计算
2)在返回值上调用CC。
3)返回的auto_ptr< int>转换为auto_ptr_ref< int>通过
使用运算符auto_ptr_ref
4)现在由于这是一个复制初始化而不是直接的初始化,因此RHS(其类型现在是auto_ptr_ref< int>)需要
转换为auto_ptr< int>再次 - 由auto_ptr< int> c / b的ctor完成,需要auto_ptr_ref< int>
5)现在应该调用CC - 但我们又回到了老问题 -
生成的auto_ptr是临时的,因此不能作为
非const引用传递给函数。
似乎我是在周期的某个地方:-(
Josuttis的书在这方面说不够 -
< quote>
机制依赖于重载
和模板参数扣除规则之间的细微差别。这种差异太微妙了
可用作一般编程工具,但足以启用
auto_ptr类才能正常工作。
< / quote>
有人可以解释一下我没有到达这里吗?
Hello All,
My question is regarding the implementation of auto pointers - How
does the class auto_ptr_ref work?
eg: suppose I have :
auto_ptr<int> foo();
int bar()
{
auto_ptr<int*> k = foo();
}
The exact sequence of calls as I see here is (no optimizations assumed)
-
1) foo returns auto_ptr<int> by value
2) CC is called on the return value.
3) The returned auto_ptr<int> is converted to auto_ptr_ref<int> by
using operator auto_ptr_ref
4) Now since this is a copy initialization and not a direct
initialization, the RHS (whose type is now auto_ptr_ref<int>) needs to
get converted to auto_ptr<int> again - done by ctor of auto_ptr<int>
which takes auto_ptr_ref<int>
5) Now CC should be called - but again we are back to old problem - the
generated auto_ptr is temporary and hence cannot be passed as a
non-const reference to the function.
Seems I am going somewhere in cycle :-(
Josuttis'' book doesnot say enough on this -
<quote>
"The mechanism relies on a slight difference between the overloading
and template argument deduction rules. This difference is too subtle to
be of use as a general programming tool, but it is sufficient to enable
the auto_ptr class to work correctly."
</quote>
Can somebody explain what I am not getting here?
推荐答案
* Neelesh Bodas:
* Neelesh Bodas:
例如:假设我有:
auto_ptr< int> foo();
int bar()
{
auto_ptr< int *> k = foo();
}
我在这里看到的确切调用顺序是(假设没有优化)
-
1)foo返回auto_ptr< int> ;按价值
OK。
2)在返回值上调用CC。
不,没有std :: auto_ptr(std :: auto_ptr const&)复制构造函数。
3)返回的auto_ptr< INT>转换为auto_ptr_ref< int>通过
使用operator auto_ptr_ref
OK。
4)现在既然是复制初始化而不是直接
初始化,RHS(其类型现在为auto_ptr_ref< int>)需要转换为auto_ptr< int>再次 - 由auto_ptr< int>
的ctor完成,它采用auto_ptr_ref< int>
好的。
5)现在CC应该叫
不,你''已达到目标。
- 但我们又回到了旧问题 -
生成的auto_ptr是临时的,因此无法作为非const传递参考函数。
eg: suppose I have :
auto_ptr<int> foo();
int bar()
{
auto_ptr<int*> k = foo();
}
The exact sequence of calls as I see here is (no optimizations assumed)
-
1) foo returns auto_ptr<int> by value
OK.
2) CC is called on the return value.
No, there is no std::auto_ptr( std::auto_ptr const& ) copy constructor.
3) The returned auto_ptr<int> is converted to auto_ptr_ref<int> by
using operator auto_ptr_ref
OK.
4) Now since this is a copy initialization and not a direct
initialization, the RHS (whose type is now auto_ptr_ref<int>) needs to
get converted to auto_ptr<int> again - done by ctor of auto_ptr<int>
which takes auto_ptr_ref<int>
OK.
5) Now CC should be called
No, you''ve reached the goal.
- but again we are back to old problem - the
generated auto_ptr is temporary and hence cannot be passed as a
non-const reference to the function.
生成auto_ptr的是什么?生成的那个是你命名为''k'的那个。
-
答:因为它弄乱了人们的顺序通常阅读文字。
问:为什么这么糟糕?
A:热门帖子。
问:什么是最多的在usenet和电子邮件中烦人的事情?
What generated auto_ptr? The one generated is the one you''ve named ''k''.
--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
你好Alf,感谢您的帮助。
实际上,我想的更多在这方面,它正在创造更多的混乱。
Hi Alf, Thanks for the help.
Actually, more I think on this, more confusion it is creating.
auto_ptr< int> foo();
int bar()
{
auto_ptr< int *> k = foo();
}
auto_ptr<int> foo();
int bar()
{
auto_ptr<int*> k = foo();
}
当函数返回值时,CC必须得到
调用是不正确的关于返回值(虽然它可能被
编译器优化了?)
不,没有std :: auto_ptr(std :: auto_ptr const&)副本构造函数。
Isn''t is correct that whever a function returns by value, a CC must get
called on the return value(Though it might be optimized away by the
compiler?)
No, there is no std::auto_ptr( std::auto_ptr const& ) copy constructor.
我相信我们不需要std :: auto_ptr(std :: auto_ptr const&)
复制构造函数 - std :: auto_ptr(std :: auto_ptr&)可以完成这项工作 -
,因为foo里面的副本来源是不一定是const:
std :: auto_ptr< int> foo()
{
std :: auto_ptr< int> v;
返回v; //复制ctor将在v上调用,因为这是返回
值
}
但是当我调试throgh std :: auto_ptr代码我没有找到任何给定拷贝构造函数的调用
(正如你在回复中所说的那样)。
我没有准确到达我错了。
I believe that we do not need a std::auto_ptr( std::auto_ptr const& )
copy constructor - std::auto_ptr( std::auto_ptr& ) would do the work -
since the source of the copy inside "foo" is not necessarily a const :
std::auto_ptr<int> foo()
{
std::auto_ptr<int> v;
return v; // a copy ctor will be called on v since this is return by
value
}
But when I debugged throgh std::auto_ptr code I didnot find any call to
the given copy constructor (as you said in the reply.)
I am not getting where exactly I am going wrong.
* Neelesh Bodas:
* Neelesh Bodas:
嗨Alf,感谢您的帮助。
Hi Alf, Thanks for the help.
Actually, more I think on this, more confusion it is creating.
auto_ptr< int> foo();
int bar()
{
auto_ptr< int *> k = foo();
}
一个函数返回值是不正确的,CC必须在返回值上调用(尽管它可能被优化掉了
编译器?)
编号_Some_构造函数必须被调用。该调用可以优化
,但构造函数必须可以调用(as-if规则
进行优化)。
不,没有std :: auto_ptr(std :: auto_ptr const&)复制构造函数。
auto_ptr<int> foo();
int bar()
{
auto_ptr<int*> k = foo();
}
Isn''t is correct that whever a function returns by value, a CC must get
called on the return value(Though it might be optimized away by the
compiler?)
No. _Some_ constructor must be called. That call can be optimized
away, but the constructor must be available to be called (the as-if rule
for optimization).
No, there is no std::auto_ptr( std::auto_ptr const& ) copy constructor.
我相信我们不需要std :: auto_ptr(std) :: auto_ptr const&)
复制构造函数 - std :: auto_ptr(std :: auto_ptr&)可以完成工作 -
因为复制源里面有foo - 不一定是const:
std :: auto_ptr< int> foo()
{
std :: auto_ptr< int> v;
返回v; //复制ctor将在v上调用,因为这是由
值返回
}
但是当我调试throgh std :: auto_ptr代码时,我没有找到任何对<的调用br />给定的拷贝构造函数(正如你在回复中所说的那样)。
I believe that we do not need a std::auto_ptr( std::auto_ptr const& )
copy constructor - std::auto_ptr( std::auto_ptr& ) would do the work -
since the source of the copy inside "foo" is not necessarily a const :
std::auto_ptr<int> foo()
{
std::auto_ptr<int> v;
return v; // a copy ctor will be called on v since this is return by
value
}
But when I debugged throgh std::auto_ptr code I didnot find any call to
the given copy constructor (as you said in the reply.)
不幸的是,这是一个不同的效果。
这里可以调用引用到非const的复制构造函数,并且我相信_is_在正式级别调用_is_(C ++抽象机器
级别),但是那个调用被你的编译器优化了。
但是,将''v'声明为const,或者使用临时的,你不再是
在可能的复制构造函数调用范围内:_some_构造函数必须是
调用(虽然它可以被优化掉),但是在const或
的情况下临时auto_ptr那个构造函数不是复制构造函数。
我不知道到底哪里出错了。
That is unfortunately a different effect.
Here the reference-to-non-const copy constructor could be called, and I
believe _is_ called at the formal level (the C++ abstract machine
level), but that call is optimized away by your compiler.
However, declare ''v'' as const, or use a temporary, and you''re no longer
in possible copy constructor call territory: _some_ constructor must be
called (although it can be optimized away), but in the case of const or
temporary auto_ptr that constructor isn''t the copy constructor.
I am not getting where exactly I am going wrong.
这次它是将编译器的优化误认为确认
误解了我写的内容(抱歉没有更多
清楚)。我并不是说从不为任何
函数结果调用复制构造函数。我的意思是复制构造函数并不总是要调用
,特别是我的意思,并写道,std :: auto_ptr
没有ref-to-const复制构造函数,这是用于函数结果的通常复制
构造函数。
-
答:因为它弄乱了人们通常阅读文字的顺序。
问:为什么这么糟糕?
A:热门发布。
问:usenet和电子邮件中最烦人的事情是什么?
This time it was mistaking the compiler''s optimization as confirmation
of a misinterpretation of what I wrote (sorry about not being more
clear). I didn''t mean that a copy constructor is never called for any
function result. I meant that a copy constructor does not always have
to be called, and in particular I meant, and wrote, that std::auto_ptr
doesn''t have a ref-to-const copy constructor, which is the usual copy
constructor used for a function result.
--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
这篇关于auto_ptr_ref的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!