
Robert Ramey skrev:
Why do I need to store the object locally first?
Of course the easy answer is that the make_nvp takes a reference and the compiler flags an error the conversion of an temporary item on the stack as an error. But of course you knew that.
The real question is, "Why does make_nvp (and other wrappers) take only references rather than copying real values". Here a couple of reasons.
a) All parameters in the serialization library are passed by reference. This is necessary to support tracking for those situations which require it. It would be confusing if support of tracking were a hidden side effect of how an argument has been passed.
b) make_nvp is a wrapper. The concept of a wrapper is to add some extra "sauce" (in this case, the external name of the data item") while leaving original operations the same. This is the case with all wrappers. That is, a wrapper doesn't change things, it just adds something. This permits one to keep in his brain what is really going on. Also it permits one to nest wrappers to any resonable depth.
c) passing by reference is going to be more efficient for larger structures.
d) not all structures have copy constructors
FWIW, Lately, I've had occasion to consider this question in more depth and can see how elaborating this issue might make the serialization library useful in new domains. However this is outside of the scope of this discussion.
After thinking a little about it I guess it is quite ok, since we don't want to serialize temporary objects by accident. I can't get the error that I though I was getting about not being able to serialize non-const objects. I guess it was just the above error all. Thanks -Thorsten