Skip to main content
GameDev.net gamedev.net
🔒 Locked

:solved: Overloading Function Template's signed-ness

Started by stylin Oct 17, 2005 at 10:56 AM 1 replies 800+ views
Original Post
stylin
stylin
EDIT: Nevermind, my types were getting mixed up inbetween writes/reads. [rolleyes] Since I've wasted your time, here's the overloaded templates for references and pointer types for your perusal: I have function templates to write and read and to/from binary streams.
namespace utility {  // generic stream reading/writing functions

   template< typename T >
   void write_to_stream( std::ostream & s, const T & source ) {
   
      s.write( reinterpret_cast< const char * >( &source ), sizeof( source ) );
      return;
   }

   template< typename T >
   void write_to_stream( std::ostream & s, T * const & source ) {
   
      s.write( reinterpret_cast< const char * >( source ), sizeof( *source ) );
      return;
   }

   template< typename T >
   void read_from_stream( std::istream & s, T & destination ) {
   
      s.read( reinterpret_cast< char * >( &destination ), sizeof( destination ) );
      return;
   }

   template< typename T >
   void read_from_stream( std::istream & s, T * const & destination ) {
   
      s.read( reinterpret_cast< char * >( destination ), sizeof( *destination ) );
      return;
   }

}

[Edited by - stylin on October 17, 2005 11:18:46 AM]
:stylin: "Make games, not war." "...if you're doing this to learn then just study a modern C++ compiler's implementation." -snk_kid
omgomghilol
omgomghilol
Just remember to only use that for POD types, otherwise, you lose;-). You also might want to somehow account for endianness and maybe make a string specialization.
stylin
stylin
Definitely.
:stylin: "Make games, not war." "...if you're doing this to learn then just study a modern C++ compiler's implementation." -snk_kid

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.