对非const的引用,以绑定到临时对象 [英] A reference to non-const to be bound to a temporary object

查看:68
本文介绍了对非const的引用,以绑定到临时对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好:


我不确定如何正确地说出这个问题,所以我先从

开始列出我的代码遇到问题:


int main()

{

std :: vector< int> x;

x.swap(std :: vector< int>());

返回0;

}


这也是不允许的:


std :: vector< int>得到()

{

返回std :: vector< int>();

}


int main()

{

std :: vector< int> x;

x.swap(get());

返回0;

}

我被告知在另一个新闻组,以下代码是

非标准C ++。原因是标准C ++

不允许将非const引用绑定到

临时对象。


但是能够将对非const的引用绑定到临时的

对象对于利用交换方法很有用。


我发现能够避免这种限制是有用的,因为它允许我包装昂贵的资源来创建

并销毁到一个类然后移动

使用交换功能最大限度地减少复制和保证异常安全

所有这些都是简洁的代码。


例如,如果Resource类包含一个昂贵的
资源,并且create static方法创建资源

,而open static方法打开资源:


//资源接口:

资源资源:: open(char * name);

资源资源:: create(char * name);


//部分代码:

资源1;

资源2;


resource1.swap (Resource :: open(" XYZ"));

resource2.swap(Resource :: create(" abc"));


I因此,我对这种限制的动机感兴趣,并且如果还有另一种方法可以同样有效地完成同样的工作,那么就会感兴趣。


-John

Hello:

I''m not sure how to word this question properly, so I''ll start by
listing the code I am having problems with:

int main()
{
std::vector<int> x;
x.swap(std::vector<int>());
return 0;
}

This is also not allowed:

std::vector<int> get()
{
return std::vector<int>();
}

int main()
{
std::vector<int> x;
x.swap(get());
return 0;
}
I was told on another newsgroup that the following code is
nonstandard C++. The reason being that "Standard C++
doesn''t allow a reference to non-const to be bound to a
temporary object."

But being able to bind a reference to non-const to a temporary
object is useful for taking advantage of using a swap method.

I find it useful to be able to avoid this restriction because
it allows me to wrap resources that are expensive to create
and destroy into a class and then move it around while
minimising copying and guaranteeing exception safety by
using a swap function, all in a concise piece of code.

For instance if the Resource class wrapped an expensive
resource and the create static method creates the resource
while the open static method opens the resource:

// Resource interface:
Resource Resource::open(char *name);
Resource Resource::create(char *name);

// Some code:
Resource resource1;
Resource resource2;

resource1.swap(Resource::open("XYZ"));
resource2.swap(Resource::create("abc"));

I''m therefore interested in the motivation for this restriction and
if there is another way to do the same job just as efficiently.

-John

推荐答案

John Ky在新闻中写道:10 *************** @ cousin.sw.oz.au:
John Ky wrote in news:10***************@cousin.sw.oz.au:
你好:

我不确定如何正确地说出这个问题,所以我先从
列出代码我遇到问题:

int main()
{st / :: std :: vector< int> x;
x.swap(std :: vector< int>());


更改为:


std :: vector< ont>()。swap(x);

返回0;
}
Hello:

I''m not sure how to word this question properly, so I''ll start by
listing the code I am having problems with:

int main()
{
std::vector<int> x;
x.swap(std::vector<int>());
change to:

std::vector<ont>().swap( x );
return 0;
}




HTH。


Rob。

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


John Ky写道:
John Ky wrote:
...
我因此对动机感兴趣这个限制和
如果还有另一种方法可以同样有效地完成同样的工作。
...
...
I''m therefore interested in the motivation for this restriction and
if there is another way to do the same job just as efficiently.
...




只是以其他方式做到这一点


std :: vector< int> x;

std :: vector< int>()。swap(x);


或在您的情况下


资源1;

资源2;


资源::打开(" XYZ")。s​​wap(resource1);

资源::创建(" abc")。s​​wap(resource2);


-

祝你好运,

Andrey Tarasevich



Just do it other way around

std::vector<int> x;
std::vector<int>().swap(x);

or in your case

Resource resource1;
Resource resource2;

Resource::open("XYZ").swap(resource1);
Resource::create("abc").swap(resource2);

--
Best regards,
Andrey Tarasevich


John Ky写道:


< snip> intro< />
John Ky wrote:

<snip> intro </>
int main()
{st / :: std :: vector< int> x;
x.swap(std :: vector< int>());
返回0;
}


< snip>更多示例代码< />

我在另一个新闻组中被告知以下代码是非标准C ++。原因是标准C ++
不允许将非const引用绑定到
临时对象。

但是能够绑定对非临时
对象的非const引用对于利用交换方法很有用。


< snip />

因此,我对此限制的动机感兴趣,如果还有其他办法可以做同样的工作同样有效。
int main()
{
std::vector<int> x;
x.swap(std::vector<int>());
return 0;
}
<snip> more example code </>
I was told on another newsgroup that the following code is
nonstandard C++. The reason being that "Standard C++
doesn''t allow a reference to non-const to be bound to a
temporary object."

But being able to bind a reference to non-const to a temporary
object is useful for taking advantage of using a swap method.
<snip/>
I''m therefore interested in the motivation for this restriction and
if there is another way to do the same job just as efficiently.




在这种特殊情况下,x.clear()可能是最有效的和简单的b $ b简洁选项。



In this particular case, x.clear( ) is probably the most efficient and
concise option.


这篇关于对非const的引用,以绑定到临时对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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