c++ - Casting char to int reference in a template -


[update: yakk pretty answered question, i'm updating question clarity. have 1 other question, mention @ end.]

i trying write serialization function, part of which, need convert char int, , leave other types unchanged. have written following code:

#include<string> #include<iostream> #include<vector>  using namespace std; template<typename t1> struct return_type {typedef t1 type;}; template<> struct return_type<char> {typedef int type;};  template<typename t1> typename return_type<t1>::type &type_transform(t1 &&t)  {     //if writing/reading char, cast int      return static_cast<typename return_type<t1>::type &>(t); }   template<typename t1> typename return_type<t1>::type &type_transform(t1 &t)  {     //if writing/reading char, cast int      return static_cast<typename return_type<t1>::type &>(t); }   char fn() {     return '\n'; }  main() {      ofstream ofs("serialized.txt");     //        ofs<<type_transform(fn()); error, should write 10     ofs.close();     ifstream ifs("serialized.txt");     //        ifs>>type_transform(b);    error, should read newline  } 

i error:

invalid static_cast type ‘char’ type ‘return_type<char>::type& {aka int&}’

questions:

  1. how can make work?
  2. how can avoid rewriting template specialization lvalue , rvalue references?

you cannot bind reference int char.

if want read type, bit more work:

template<class t> struct io_storage {   t& t;   io_storage(io_storage&&)=default;   io_storage(t& tin):t(tin) {} }; template<class t, class=void, class storage=io_storage<t>> struct io_read_helper_t:storage {   friend std::istream& operator>>( std::istream& i, io_read_helper_t x ) {     return >> x.t;   }   using storage::storage; }; template<class t, class=void, class storage=io_storage<t>> struct io_write_helper_t:storage {   friend std::ostream& operator<<( std::ostream& o, io_write_helper_t x ) {     return o << x.t;   }   using storage::storage; }; template<class t, class impl=io_write_helper_t<t, void, io_read_helper_t<t>>> struct io_helper_t : impl {   using impl::impl; };  template<class t> io_helper_t<t> io_helper( t& t ) { return {t}; } template<class t> io_write_helper_t<const t> io_helper( t const& t ) { return {t}; } template<class t> io_write_helper_t<const t> io_helper( t&& t ) { return {t}; }  template<class...>struct voider{using type=void;}; template<class...ts>using void_t=typename voider<ts...>::type;  template<class t> struct io_type_as {}; template<> struct io_type_as<char>{ using type=int; };  template<class t> using io_type_as_t=typename io_type_as<typename std::remove_const<t>::type>::type;  template<class t, class storage> struct io_read_helper_t<t, void_t<io_type_as_t<t>>, storage>:storage {   using x=io_type_as_t<t>;   friend std::istream& operator>>( std::istream& i, io_read_helper_t x ) {     x tmp;     auto& r = >> io_helper(tmp);     x.t = std::move(tmp);     return r;   }   using storage::storage; }; template<class t, class storage> struct io_write_helper_t<t, void_t<io_type_as_t<t>>, storage>:storage {   using x=io_type_as_t<t>;   friend std::ostream& operator<<( std::ostream& o, io_write_helper_t x )   {     return o << io_helper(x(x.t));   }   using storage::storage; }; 

now, if define io_type_as<x>::type y, iohelper(x) automatically read/write type y, , assigned before/after needed.

live example


Comments

Popular posts from this blog

1111. appearing after print sequence - php -

java - WARN : org.springframework.web.servlet.PageNotFound - No mapping found for HTTP request with URI [/board/] in DispatcherServlet with name 'appServlet' -

Ruby on Rails, ActiveRecord, Postgres, UTF-8 and ASCII-8BIT encodings -