serialization::make_nvp parameters
Hello, I am hoping someone could answer a question I have regarding the make_nvp(..) function of the Serialization library. I am attempting to serialize a number of objects within a loop, and the name of the name-value-pair is to be assigned dynamically depending on some values taken from within the object that is to be serialized. I am currently performing the actions below: my_obj = GetMyObj(..); std::string temp = str( boost::format("x:%1% y:%2%") % _x % _y ); const char* const_temp = temp.c_str( ); ar & boost::serialization::make_nvp(const_temp, my_obj); This code compiles but will cause an exception on make_nvp. As far as I can tell, I am providing the function with the const char* that it requires, so am very confused as to why it is not performing as expected. Any help with this would be greatly appreciated. John Hammond ******************************************************************** This email and any attachments are confidential to the intended recipient and may also be privileged. If you are not the intended recipient please delete it from your system and notify the sender. You should not copy it or use it for any purpose nor disclose or distribute its contents to any other person. ********************************************************************
On Tue, 08 Sep 2009 13:16:49 +0200, Hammond, John (UK New Malden)
Hello,
I am hoping someone could answer a question I have regarding the make_nvp(..) function of the Serialization library.
I am attempting to serialize a number of objects within a loop, and the name of the name-value-pair is to be assigned dynamically depending on some values taken from within the object that is to be serialized. I am currently performing the actions below:
my_obj = GetMyObj(..);
std::string temp = str( boost::format("x:%1% y:%2%") % _x % _y );
probably it is the presence of colons in the string? Try removing them and retry. Greetings, Luca
AMDG Hammond, John (UK New Malden) wrote:
I am hoping someone could answer a question I have regarding the make_nvp(..) function of the Serialization library.
I am attempting to serialize a number of objects within a loop, and the name of the name-value-pair is to be assigned dynamically depending on some values taken from within the object that is to be serialized. I am currently performing the actions below:
my_obj = GetMyObj(..);
std::string temp = str( boost::format("x:%1% y:%2%") % _x % _y );
The space may be a problem. Boost.Serialization sticks the
name directly into the XML, and
const char* const_temp = temp.c_str( );
ar & boost::serialization::make_nvp(const_temp, my_obj);
This code compiles but will cause an exception on make_nvp. As far as I can tell, I am providing the function with the const char* that it requires, so am very confused as to why it is not performing as expected.
In Christ, Steven Watanabe
I would not expect this to work.
const char* const_temp = temp.c_str( );
provides a cont char * - but this pointer is only temporary and becomes
invalid when the function creating it returns. Hence the run time error.
Assuming that dynamically assigning a tag name is a good idea (I'll reserver
judgement on this for now), I don't think that nvp is the right vehicle to
try to do this. I would guess that you'd have to make your own "wrapper"
which makes a copy of the "tag". The nvp specifically avoids doing this for
efficiency reasons
Robert Ramey
"Hammond, John (UK New Malden)"
On Tue, 08 Sep 2009 19:35:06 +0200, Robert Ramey
I would not expect this to work.
const char* const_temp = temp.c_str( );
provides a cont char * - but this pointer is only temporary and becomes invalid when the function creating it returns. Hence the run time error.
I believe const_temp is valid here, up to exit of code block it resides. Perhaps you confused this code with the following one, which is wrong because the temporary 'str' std::string is then destroyed just after it has been created, i.e.: const char* const_temp = str( boost::format("x:%1% y:%2%") % _x % _y ).c_str( ); and const_temp points to a data buffer of an already destroyed std::string. Greetings, Luca
participants (4)
-
Hammond, John (UK New Malden)
-
Luca Cappa
-
Robert Ramey
-
Steven Watanabe