在模板中将char转换为int引用

Casting char to int reference in a template

本文关键字:转换 int 引用 char      更新时间:2023-10-16

[UPDATE: Yakk基本上回答了我的问题,但为了清楚起见,我正在更新这个问题。]我还有一个问题,我在最后提到了。

我正在尝试写一个序列化函数,作为其中的一部分,我需要将char转换为int,并保持其他类型不变。我写了下面的代码:

#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 it to 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 it to 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 back
}

我得到一个错误:

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

问题:

  1. 我怎样才能使它工作?
  2. 如何避免为左值和右值引用重写模板专门化?

不能绑定intchar的引用。

如果你想读取另一种类型的内容,做更多的工作:

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 i >> 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 = i >> 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;
};

现在,如果您将io_type_as<X>::type定义为Y,则iohelper(x)将自动以Y类型读写,并根据需要在前/后分配。

生活例子

你需要重新考虑你的设计。

static_cast将不允许将charchar&转换为int&。这就是在下面的调用中将要发生的事情:

f1>>type_transform(variable);
如果variablechar的类型