Talk About Network

Google


Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Programming > C++ Moderated > Re: is this a r...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 1 of 1 Topic 9292 of 10104
Post > Topic >>

Re: is this a reference to a tem****ary or not?

by Bart van Ingen Schenau <bart@[EMAIL PROTECTED] > Feb 17, 2008 at 09:22 PM

Joshua Lehrer wrote:

> Does the following invoke undefined behavior? 

Yes.

> The question is whether 
> or not a tem****ary is created in the call to "cast".  If so, then cast
> is returning a reference to a tem****ary, and thus "Const" is returning
> a reference to a tem****ary which then falls out of scope.

Yes, a tem****ary is created.

> 
> The attempt is to convert "unsigned int *" to "const unsigned int *"
> via a function call.  Please, I know, use const_cast, etc...  This is
> a very boiled down example of a much larger problem.
> 
> 
> #include <iostream>
> 
> typedef const unsigned int * const ret_type;
> 
> static inline ret_type& cast(ret_type &param) { return param; }
> 
> inline ret_type & Const(unsigned int * const & param) { return
> cast(param); }
> 
>   inline void print(const unsigned int * const start,
>                        const unsigned int * const finish) {
>    std::cout << start << ' ' << finish << std::endl;
> }
> 
> int main()
> {
>    unsigned int arr[2] = {42, 43};
>    print(&arr[0], &arr[1]);
>    print(Const(&arr[0]), Const(&arr[1]));

The address-of operator (&) yields an r-value of the non-class
type 'pointer to unsigned int'.
When binding a non-class type r-value to a reference, the standard
states quite clearly in section [decl.init.ref]/5, last bullet that a
tem****ary gets created.

> }
> 
> 
> thanks,
> 
> joshua
> 
Bart v Ingen Schenau
-- 
a.c.l.l.c-c++ FAQ: http://www.comeaucomputing.com/learn/faq
c.l.c FAQ: http://c-faq.com/
c.l.c++ FAQ: http://www.para****ft.com/c++-faq-lite/

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




 1 Posts in Topic:
Re: is this a reference to a temporary or not?
Bart van Ingen Schenau &l  2008-02-17 21:22:05 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Wed Oct 15 22:42:37 CDT 2008.