Topic: Explicit/implicit unnamed temporaries and rvalues
Author: macke@yar.nu (Marcus Lindblom)
Date: Tue, 3 Jul 2007 13:07:31 GMT Raw View
Hi,
(Sorry if this has been discussed before, but I've looked around for a
good answer and failed.)
IIUC, there is no difference between an implicit temporary and an
explicit temporary, both are treated as rvalues? Why is that?
I.e. for:
void foo(double &val) { ... }
void bar()
{
int my_val = ...;
foo(my_val); // invalid
foo(double(my_val)); // also invald, but why?
}
The reason I'm asking, is that, implicitly converted temporaries are
rvalues because we don't want the compiler to create a temporary that
gets modified without the user being aware of it.
But for explicit temporaries, it seems to me that it is more obvious to
the user, and thus could be allowed?
Or are there other dark corners lurking here? I'm just curious. :)
Cheers,
/Marcus
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html ]
Author: Danping <dpzou@fudan.edu.cn>
Date: Wed, 4 Jul 2007 12:09:11 CST Raw View
Marcus Lindblom :
> Hi,
>
> (Sorry if this has been discussed before, but I've looked around for a
> good answer and failed.)
>
> IIUC, there is no difference between an implicit temporary and an
> explicit temporary, both are treated as rvalues? Why is that?
>
> I.e. for:
>
> void foo(double &val) { ... }
>
> void bar()
> {
> int my_val = ...;
> foo(my_val); // invalid
> foo(double(my_val)); // also invald, but why?
> }
>
> The reason I'm asking, is that, implicitly converted temporaries are
> rvalues because we don't want the compiler to create a temporary that
> gets modified without the user being aware of it.
>
> But for explicit temporaries, it seems to me that it is more obvious to
> the user, and thus could be allowed?
>
> Or are there other dark corners lurking here? I'm just curious. :)
>
> Cheers,
> /Marcus
>
> ---
> [ comp.std.c++ is moderated. To submit articles, try just posting with ]
> [ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
> [ --- Please see the FAQ before posting. --- ]
> [ FAQ: http://www.comeaucomputing.com/csc/faq.html ]
>
I'am also confused about the standard that it's illegal to bind
non-const reference to temporary object. The reason that to modify the
content of the temporary object makes no sense seems reasonable, but
this also forbids the access the non-const functions of the temporary
object, while in some cases, it becomes neccessary.
I once wrote a math library with vc++7.0 compiler which doesn't strictly
follow the iso c++ standard. So the non-const reference can be binded to
temporary object. For the reason of high efficiency, I wrote most of
algorithms with reference parameters. It works well with the propery of
non-const reference binding to temporary object.
i.e. I wrote a class 'vec' like this
class vec
{
proteced:
size_t _sz;
double* _data;
public:
vec sub_vec(size_t i0,size_t len)
{
//return a temporary object to manage the sub-sequence.
vec tmp;
tmp._sz = len;
tmp._data = _data+i0;
return tmp;
}
};
and I wrote algorithms containing non-const reference like this,
void do_some_thing(vec& in_out)//reference is more efficient
{
//may call some non-const function to change the value of blocks
//pointed by vec._data
}
It is very convenient to operate on the sub-sequence like that
do_some_thing( v.sub_vec(2,5)).
However, when I use the gcc, compilation fails in non-const reference
binding to temporary variables.
So i think it's not a good idea to forbid the non-const reference to a
temporary objects.
I'm also looking foward to a better explan for this.
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html ]
Author: Howard Hinnant <howard.hinnant@gmail.com>
Date: Wed, 4 Jul 2007 15:18:24 CST Raw View
In article <468B6012.2040502@fudan.edu.cn>,
Danping <dpzou@fudan.edu.cn> wrote:
> Marcus Lindblom :
> > Hi,
> >
> > (Sorry if this has been discussed before, but I've looked around for a
> > good answer and failed.)
> >
> > IIUC, there is no difference between an implicit temporary and an
> > explicit temporary, both are treated as rvalues? Why is that?
> >
> > I.e. for:
> >
> > void foo(double &val) { ... }
> >
> > void bar()
> > {
> > int my_val = ...;
> > foo(my_val); // invalid
> > foo(double(my_val)); // also invald, but why?
> > }
> >
> > The reason I'm asking, is that, implicitly converted temporaries are
> > rvalues because we don't want the compiler to create a temporary that
> > gets modified without the user being aware of it.
> >
> > But for explicit temporaries, it seems to me that it is more obvious to
> > the user, and thus could be allowed?
> >
> > Or are there other dark corners lurking here? I'm just curious. :)
> >
> > Cheers,
> > /Marcus
> >
> > ---
> > [ comp.std.c++ is moderated. To submit articles, try just posting with ]
> > [ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
> > [ --- Please see the FAQ before posting. --- ]
> > [ FAQ: http://www.comeaucomputing.com/csc/faq.html ]
> >
> I'am also confused about the standard that it's illegal to bind
> non-const reference to temporary object. The reason that to modify the
> content of the temporary object makes no sense seems reasonable, but
> this also forbids the access the non-const functions of the temporary
> object, while in some cases, it becomes neccessary.
>
>
> I once wrote a math library with vc++7.0 compiler which doesn't strictly
> follow the iso c++ standard. So the non-const reference can be binded to
> temporary object. For the reason of high efficiency, I wrote most of
> algorithms with reference parameters. It works well with the propery of
> non-const reference binding to temporary object.
>
> i.e. I wrote a class 'vec' like this
>
> class vec
> {
> proteced:
> size_t _sz;
> double* _data;
>
> public:
> vec sub_vec(size_t i0,size_t len)
> {
> //return a temporary object to manage the sub-sequence.
> vec tmp;
> tmp._sz = len;
> tmp._data = _data+i0;
> return tmp;
> }
>
> };
>
> and I wrote algorithms containing non-const reference like this,
>
> void do_some_thing(vec& in_out)//reference is more efficient
> {
> //may call some non-const function to change the value of blocks
> //pointed by vec._data
> }
>
> It is very convenient to operate on the sub-sequence like that
>
> do_some_thing( v.sub_vec(2,5)).
>
>
> However, when I use the gcc, compilation fails in non-const reference
> binding to temporary variables.
>
> So i think it's not a good idea to forbid the non-const reference to a
> temporary objects.
>
> I'm also looking foward to a better explan for this.
Fwiw, C++0X will allow you to do what you want with slightly different syntax:
void do_some_thing(vec&& in_out)//reference is more efficient
{
//may call some non-const function to change the value of blocks
//pointed by vec._data
}
This now compiles:
do_some_thing( v.sub_vec(2,5)).
Search for "rvalue reference".
-Howard
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html ]