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 ¶m) { 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! ]


|