检查参数是否是临时的 [英] Check if an argument is temporary

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

问题描述

大家好,


我想知道如果你可以检查一个函数,如果一个

的参数是临时的话。 />

我的意思是以下内容。 A是一个类,foo是一个函数返回

a类和bar是一个函数使用A是一个参数返回一些东西

其他


A级;

A foo(无效);

int bar(const A&);


int main( void){

std :: cout<< bar(foo());

返回0;

}


在上面的代码中,返回的对象是棒是暂时的,并且在棒完成后销毁了



问题是,如果它的参数是

这样一个临时对象?


问候

Klaas

Hi all,

I was wondering if it is possible if you can check in a function if one
of the arguments is temporary.

What I mean is the following. A is a class, foo is a function returning
a class and bar is a function using A is an argument returning something
else

class A;
A foo(void);
int bar(const A&);

int main(void) {
std::cout << bar(foo());
return 0;
}

In the above piece of code, the object returned by foo is temporary and
destroyed after bar is finished with it.

The question is, is it possible to distinguish in bar if its argument is
such a temporary object or not?

Regards
Klaas

推荐答案

Klaas Vantournhout< no ************ @ spam.comwrote:
Klaas Vantournhout <no************@spam.comwrote:

我想知道是否有可能如果你可以检查一个函数,如果一个参数的b $ b b是暂时的。


我的意思是以下内容。 A是一个类,foo是一个函数返回

a类和bar是一个函数使用A是一个参数返回一些东西

其他


A级;

A foo(无效);

int bar(const A&);


int main( void){

std :: cout<< bar(foo());

返回0;

}


在上面的代码中,返回的对象是棒是暂时的,并且在棒完成后销毁了



问题是,如果它的参数是

这样的临时对象与否?
I was wondering if it is possible if you can check in a function if one
of the arguments is temporary.

What I mean is the following. A is a class, foo is a function returning
a class and bar is a function using A is an argument returning something
else

class A;
A foo(void);
int bar(const A&);

int main(void) {
std::cout << bar(foo());
return 0;
}

In the above piece of code, the object returned by foo is temporary and
destroyed after bar is finished with it.

The question is, is it possible to distinguish in bar if its argument is
such a temporary object or not?



不可移植。如果你对你所使用的系统有足够的了解,你可以从地址告诉它是否在堆中或堆栈中,但那是'/ b
它。


一个明显的解决方案是在''bar()'中有一个单独的参数

告诉函数传入的对象是否是温度与否。 :-)

Not portably. If you know enough about the system you are on, you can
tell from the address if it is in the heap or on the stack, but that''s
about it.

An obvious solution would be to have a separate argument in ''bar()'' to
tell the function if the object passed in is a temp or not. :-)


10月29日凌晨3:29,Daniel T. < danie ... @ earthlink.netwrote:
On Oct 29, 3:29 am, "Daniel T." <danie...@earthlink.netwrote:

Klaas Vantournhout< no_valid_em ... @ spam.comwrote:
Klaas Vantournhout <no_valid_em...@spam.comwrote:

我想知道如果你可以签入一个函数,如果一个

的参数是临时的,那么是否可能。
I was wondering if it is possible if you can check in a function if one
of the arguments is temporary.


我的意思是以下内容。 A是一个类,foo是一个函数返回

a类和bar是一个函数使用A是一个参数返回一些东西

else
What I mean is the following. A is a class, foo is a function returning
a class and bar is a function using A is an argument returning something
else


class A;

A foo(void);

int bar( const A&);
class A;
A foo(void);
int bar(const A&);


int main(void){

std :: cout << bar(foo());

返回0;

}
int main(void) {
std::cout << bar(foo());
return 0;
}


在上面的代码中,foo返回的对象是临时的,并且在bar完成后销毁了


In the above piece of code, the object returned by foo is temporary and
destroyed after bar is finished with it.


问题是,是否有可能在bar中区分其参数是否为

这样的临时对象与否?
The question is, is it possible to distinguish in bar if its argument is
such a temporary object or not?


不可移植。如果你对你所使用的系统有足够的了解,你可以从地址告诉它是否在堆中或堆栈中,但那是'/ b
它。
Not portably. If you know enough about the system you are on, you can
tell from the address if it is in the heap or on the stack, but that''s
about it.



这没有用,因为局部变量也在堆栈上。

That wouldn''t help, since local variables are also on the stack.


一个明显的解决方案是在

''bar()''中有一个单独的参数来告诉函数传入的对象是否为临时值
。 :-)
An obvious solution would be to have a separate argument in
''bar()'' to tell the function if the object passed in is a temp
or not. :-)



在const引用和非const引用上重载可能

就足够了。一个const变量仍将绑定到const

引用,就像一个临时变量,但非const变量将

绑定到非const引用。


真正的问题是OP想知道的原因。对于他真正的问题,可能有一个

的解决方案,这不需要这样的知识。


-

James Kanze(GABI软件)电子邮件:ja ********* @ gmail.com

Conseils eninformatiqueorientéeobjet/

Beratung in objektorientierter Datenverarbeitung

9placeSémard,78210 St.-Cyr-l''coco,France,+ 33(0)1 30 23 00 34

Overloading on a const reference and a non-const reference might
be sufficient. A const variable will still bind to the const
reference, like a temporary, but a non-const variable will bind
to the non-const reference.

The real question is why the OP wants to know. There''s likely a
solution to his real problem which doesn''t require such
knowledge.

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l''école, France, +33 (0)1 30 23 00 34


James Kanze写道:
James Kanze wrote:

真正的问题是OP想知道的原因。对于他真正的问题,可能有一个解决方案,而且不需要这样的知识。
The real question is why the OP wants to know. There''s likely a
solution to his real problem which doesn''t require such
knowledge.



嗯,这是我的想法。


假设你有一个班级阵列


类数组{

public:

array(int N):n(N){

if(n) a = new double [N];

else a = NULL;

}


~array(void){delete [ ] 一个; }


数组运算符=(const数组& A){

if(n!= An){delete [] a;

a = new double [An]; }

for(注册int i = N-1; i> = 0; --i)

a [i] = Aa [i];


返回* this;

}

private

double * a;

int n;

}


现在假设我们有函数

array foo(void);

创造了一个庞大的阵列


然后进行以下操作


阵列B;

B = foo( );


产生巨大的开销。


在foo()中创建临时数组,此数组传递给operator =

并且foo的每个元素都被复制到B.


但是,如果operator =可以注意到对象是否为

临时。

因为那时我们可以写(如果)(临时){

a = Aa;

Aa = NULL;

}


当发生这种情况时,foo会在operator =之后被销毁,但它只是一个

NULL指针,真实日期仍然存在而不是删除~array(void);


当然我在这里看到一个问题我定义了operator =(const array&),

这意味着我无法改变它的价值!


简单的解决方案显然会重新定义

array foo(void)

to

void foo(array&)

但遗憾的是这不是一个选项。

Of当然,如果

编译器的优化标志检查内存地址的内容是否只是移动到其他

地址,上述情况就不会有问题。我不知道这里有任何组装,但我的意思是

跟随。假设地址a由function1填充并包含

临时数据,并且该数据由function2放在地址b中。然后

我不知道优化标志是否看到这个,并立即将函数1的

数据存储在地址b中,而不是绕过临时a。

问候

Klaas

Hmm okay this is what I had in mind.

Assume you have a class array

class array {
public:
array(int N) : n(N) {
if (n) a = new double[N];
else a = NULL;
}

~array(void) { delete [] a; }

array operator=(const array &A) {
if (n != A.n) { delete [] a;
a = new double [A.n]; }
for (register int i=N-1; i >= 0; --i)
a[i] = A.a[i];

return *this;
}
private
double *a;
int n;
}

Assume now we have the function
array foo(void);
which creates an enormous array

Then the following operation

array B;
B = foo();

Creates a tremendous amount of overhead.

A temporary array is created in foo(), this array is passed to operator=
and there each element of foo is copied to B.

However, it would be useful if operator= could notice if the object is
temporary.
Because then we could write

if (temporary) {
a = A.a; A.a = NULL;
}

When this happens, foo gets destroyed after operator=, but it is only a
NULL pointer, the real date is still there and not delete with ~array(void);

Of course I see a problem here there I defined operator=(const array &),
which means that I can not change A.a its value!

The simple solution would obviously be redefining
array foo(void)
to
void foo(array &)
but this is unfortunately not an option here.
Of course the above would not be a problem if optimization flags of
compilers check if content a memory address is just moved to an other
address. I don''t know any assembly here, but what I mean is the
following. Assume address a is filled by function1 and contains
temporary data, and this data is placed by function2 in address b. Then
I don''t know if optimization flags see this, and immediately store the
data of function1 in address b, instead of going around over temporary a.
Regards
Klaas


这篇关于检查参数是否是临时的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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