Fixes empty string serialization for debug in VS (#69)

Previously we took the address of the dereferenced begin() iterator
on a string to get the pointer to its data; we now just const_cast
the pointer that is the result of calling data().

The original reason for using the iterator over something like data()
was to avoid the const_cast and to ensure that any copy on write
mechanisms were used, but this doesn't seem necessary given that we
call resize() immediately prior to this.  Valgrind shows no problems
with the new method.  Also added unit tests for this case to string.
This commit is contained in:
Shane Grant 2014-03-08 11:08:48 -08:00
parent 196822ef9f
commit 970aa9f07d
3 changed files with 8 additions and 3 deletions

View File

@ -53,7 +53,7 @@ namespace cereal
size_type size;
ar( make_size_tag( size ) );
str.resize(static_cast<std::size_t>(size));
ar( binary_data( &(*str.begin()), static_cast<std::size_t>(size) * sizeof(CharT) ) );
ar( binary_data( const_cast<CharT *>( str.data() ), static_cast<std::size_t>(size) * sizeof( CharT ) ) );
}
} // namespace cereal

View File

@ -32,7 +32,7 @@
#include <cereal/types/vector.hpp>
#include <cereal/archives/binary.hpp>
//#include <cereal/archives/portable_binary.hpp>
#include <cereal/archives/portable_binary.hpp>
#include <cereal/archives/xml.hpp>
#include <cereal/archives/json.hpp>

View File

@ -1892,24 +1892,29 @@ void test_string_basic()
for(size_t i=0; i<100; ++i)
{
std::basic_string<char> o_string = random_basic_string<char>(gen);
std::basic_string<char> o_string = random_basic_string<char>(gen);
std::basic_string<char> o_string2 = "";
std::ostringstream os;
{
OArchive oar(os);
oar(o_string);
oar(o_string2);
}
std::basic_string<char> i_string;
std::basic_string<char> i_string2;
std::istringstream is(os.str());
{
IArchive iar(is);
iar(i_string);
iar(i_string2);
}
BOOST_CHECK_EQUAL(i_string, o_string);
BOOST_CHECK_EQUAL(i_string2, o_string2);
}
}