From 1aee468deafb447ebda1b6034c5233f13a5058c5 Mon Sep 17 00:00:00 2001 From: Benny Baumann Date: Sun, 22 Feb 2015 15:46:27 +0100 Subject: [PATCH 1/1] fix: Use a less arcane way of typecasting from char[] to T --- src/io/record.h | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/src/io/record.h b/src/io/record.h index 27c40c1..8857665 100644 --- a/src/io/record.h +++ b/src/io/record.h @@ -57,13 +57,21 @@ public: } template - static void read( std::string::iterator& it, T&& val ) { - char* data = reinterpret_cast( &val ); + static void read( std::string::const_iterator& it, T& val ) { + union typeConversion { + char buf[sizeof(T)]; + T value; + + typeConversion(const T& v) : value(v) {} + }; + + typeConversion data( 0 ); for( size_t i = 0; i < sizeof( T ); i++ ) { - data[i] = *it; - it++; + data.buf[i] = *it++; } + + val = data.value; } std::string packToString() { @@ -79,12 +87,12 @@ public: return res; } - void unpackFromString( std::string str ) { + void unpackFromString( const std::string& str ) { if( str.size() != RECORD_HEADER_SIZE ) { throw "Invalid string length"; } - auto it = str.begin(); + auto it = str.cbegin(); read( it, command ); read( it, flags ); read( it, sessid ); -- 2.39.2