From 970aa9f07d77f10444bd78367dbc6eaafe743e9a Mon Sep 17 00:00:00 2001 From: Shane Grant Date: Sat, 8 Mar 2014 11:08:48 -0800 Subject: [PATCH] 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. --- include/cereal/types/string.hpp | 2 +- sandbox_vs.cpp | 2 +- unittests.cpp | 7 ++++++- 3 files changed, 8 insertions(+), 3 deletions(-) diff --git a/include/cereal/types/string.hpp b/include/cereal/types/string.hpp index 94c6d661c..5dbf35cdf 100644 --- a/include/cereal/types/string.hpp +++ b/include/cereal/types/string.hpp @@ -53,7 +53,7 @@ namespace cereal size_type size; ar( make_size_tag( size ) ); str.resize(static_cast(size)); - ar( binary_data( &(*str.begin()), static_cast(size) * sizeof(CharT) ) ); + ar( binary_data( const_cast( str.data() ), static_cast(size) * sizeof( CharT ) ) ); } } // namespace cereal diff --git a/sandbox_vs.cpp b/sandbox_vs.cpp index 39d6cb090..2b80c057c 100644 --- a/sandbox_vs.cpp +++ b/sandbox_vs.cpp @@ -32,7 +32,7 @@ #include #include -//#include +#include #include #include diff --git a/unittests.cpp b/unittests.cpp index 468a8e1a9..2ea49faec 100644 --- a/unittests.cpp +++ b/unittests.cpp @@ -1892,24 +1892,29 @@ void test_string_basic() for(size_t i=0; i<100; ++i) { - std::basic_string o_string = random_basic_string(gen); + std::basic_string o_string = random_basic_string(gen); + std::basic_string o_string2 = ""; std::ostringstream os; { OArchive oar(os); oar(o_string); + oar(o_string2); } std::basic_string i_string; + std::basic_string 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); } }