[C++-sig] Re: contained structure access

Graeme Lufkin gwl at u.washington.edu
Fri Nov 8 00:58:15 CET 2002

>So, did you look at this line?
>> `sizeof' applied to incomplete type
>The source line is:
>        BOOST_STATIC_ASSERT(is_class<T>::value);
>Which means you can only return a reference to an object of class
>type. Since double is not of class type, it complains.
>Why don't you use return_value_policy<copy_non_const_reference>
	Aha! I have (sort of) seen the light.  I think I'm beginning to
understand these return_value_policy<> things.  So I want my function to
return a reference to a built-in type.  But I want the actual object,
not a copy, so that I can assign it to something.  So I want
reference_existing_object.  However, this also only works on objects
(same error as before).  I tried using copy_non_const_reference,
expecting that it would be assignable, but the change would be made to
the copy, and thus not appear in the original object.  However, when I
tried to assign like this 'at[0] = 3', I got:
TypeError: object doesn't support item assignment
which confuses me again.

>> The other question: I'm still not sure I understand the business with
>> properties and return_internal_reference<>.  I don't get why the
>> level of member-ness (i.e. x.y.z) reveals the copy business, which
>> doesn't appear one level lower (i.e. x.y).  But I can make it work,
>> maybe I'll just forget it.
>Just remember that when reading a property, the result is returned by
>value unless you specify otherwise. So x.y.z = 1 becomes
>     setattr(getattr(x, "y"), "z", 1)
>If you don't do something explicit to prevent it, getattr(x, "y")
>returns a copy of x's y member, so assigning to its z element changes
>the wrong object.
	Aha again! I was not aware of the setattr() business.  My picture of
the situation was 'x.y = 2' becomes 'set(getattr(x, "y"), 2', so I
thought the problem would have arisen in that call to getattr(). 
Instead it's 'setattr(x, "y", 2)' which works.  Thanks for bearing with
	- Graeme Lufkin
	gwl at u.washington.edu
"The experiment was inconclusive, so we had to use statistics."

More information about the Cplusplus-sig mailing list