Connecting Tech Pros Worldwide Forums | Help | Site Map

C++ Templates

huangshan
Guest
 
Posts: n/a
#1: Oct 23 '06
hi all:

i am reading "C++ Templates: The Complete Guide"

in "2.4 Overloading Function Templates",
he say "This is because for C-strings, max(a,b) creates a new, temporary
local value that may be returned by the function by reference."

i can't understand that why in this condition will becomes an error.



Victor Bazarov
Guest
 
Posts: n/a
#2: Oct 23 '06

re: C++ Templates


huangshan wrote:
Quote:
i am reading "C++ Templates: The Complete Guide"
>
in "2.4 Overloading Function Templates",
he say "This is because for C-strings, max(a,b) creates a new,
temporary local value that may be returned by the function by
reference."
i can't understand that why in this condition will becomes an error.
Returning a reference to a [local] temporary object is something you
should consider avoiding since the reference will outlive the object
and will become invalid before the function even returns it. Any
use of that reference will have undefined behaviour.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask


huangshan
Guest
 
Posts: n/a
#3: Oct 23 '06

re: C++ Templates


can you give me a example ?

thanks


Ye Dafeng
Guest
 
Posts: n/a
#4: Oct 23 '06

re: C++ Templates


huangshan wrote:
Quote:
can you give me a example ?
>
thanks
>
>
char* getVoid()
{
char temp = 'c';

return &temp;
}

in this example, the temp address may be used for other object. If you
use it, you will get another result which you do not expect.
Greg Comeau
Guest
 
Posts: n/a
#5: Oct 23 '06

re: C++ Templates


In article <ehikci$l4s$1@news.cn99.com>,
Ye Dafeng <kobe082002@gmail.comwrote:
Quote:
>huangshan wrote:
Quote:
>can you give me a example ?
>>
>thanks
>>
>>
>
>char* getVoid()
>{
> char temp = 'c';
>
> return &temp;
>}
>
>in this example, the temp address may be used for other object. If you
>use it, you will get another result which you do not expect.
Right, as Victor mentioned, temp is not guaranteed to exist once
getVoid is over with. Therefore, to return its adress to be used
as if it still exists is asking for trouble. This is another facet
of something I term "good garbage" because it is something that
may appear to work, but use that pointer 10 minutes lates and
it may not be pointing at temp any longer but some other variable
which has taken its place, and so on.
--
Greg Comeau / 20 years of Comeauity! Intel Mac Port now in beta!
Comeau C/C++ ONLINE == http://www.comeaucomputing.com/tryitout
World Class Compilers: Breathtaking C++, Amazing C99, Fabulous C90.
Comeau C/C++ with Dinkumware's Libraries... Have you tried it?
loufoque
Guest
 
Posts: n/a
#6: Oct 23 '06

re: C++ Templates


Ye Dafeng wrote:
Quote:
huangshan wrote:
Quote:
>can you give me a example ?
>>
>thanks
>>
>>
>
char* getVoid()
{
char temp = 'c';
>
return &temp;
}
This is not what he asked for.
What he asked for is :

char& getVoid()
{
char temp = 'c';

return temp;
}
Martin Steen
Guest
 
Posts: n/a
#7: Oct 23 '06

re: C++ Templates


huangshan wrote:
Quote:
can you give me a example ?
>
thanks
>
>
Don't do this:

int& Function1()
{
int a = 100;
return a; // g++ warning: reference to local variable 'a' returned
// When Function1 ends, the reference to a is no longer
// valid, because a is local.
}

void Function2()
{
cout << Function1() << endl;
// output is not "100", but something else!
}

Best regards,
-Martin
huangshan
Guest
 
Posts: n/a
#8: Oct 24 '06

re: C++ Templates


thanks
Victor Bazarov ,
Ye Dafeng,
Greg Comeau,
loufoque
and Martin Steen

i see


Closed Thread