From: Larry Evans on
The code shown below produces output:

fun<0>(.).val()=100
fun<1>(.).val()=-926642400

when compiled with:

ftp://gcc.gnu.org/pub/gcc/snapshots/4.5-20100408

The odd value for fun<1> output is most probably produced
by premature destruction of the concrete returned from
fun<1> call.

How should the code be changed to get output 100
on the 2nd line?

TIA.

-Larry


//== "code shown below" ==
//Purpose:
// See if function can return rvalue-reference of absstract class.
//

#include <iostream>
struct abstract
{
virtual int
val(void)const
=0
;
};
struct concrete
: public abstract
{
int
my_val
;
concrete(int a_val)
: my_val(a_val)
{}
int
val(void)const
{
return my_val;
}
};
template
< int Id
>
abstract&&
fun
( abstract&& a
)
{
return std::forward<abstract>(concrete(a.val()));
}
template
<
>
abstract&&
fun
< 0//Id
>
( abstract&& a
)
{
return std::forward<abstract>(a);
}
int main(void)
{
static int const val=100;
{
static int const id=0;

std::cout<<"fun<"<<id<<">(.).val()="<<fun<id>(std::forward<abstract>(concrete(val))).val()<<"\n";
}
{
static int const id=1;

std::cout<<"fun<"<<id<<">(.).val()="<<fun<id>(std::forward<abstract>(concrete(val))).val()<<"\n";
}
return 0;
}

--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

From: Paul Bibbings on
Larry Evans <cppljevans(a)gmail.com> writes:

> The code shown below produces output:
>
> fun<0>(.).val()=100
> fun<1>(.).val()=-926642400
>
> when compiled with:
>
> ftp://gcc.gnu.org/pub/gcc/snapshots/4.5-20100408

<snipped code />

For comparison, I get:

03:21:43 Paul Bibbings(a)JIJOU
/cygdrive/d/CPPProjects/CLCPPM $./premature_destruction
fun<0>(.).val()=100
fun<1>(.).val()=100

from your code, when built with both gcc-4.4.3 and gcc-4.5.0-20100128
(Cygwin, Windows OS).

Regards

Paul Bibbings

--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]