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

标签 c++ templates c++11

[更新: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. 如何避免重写左值和右值引用的模板特化?

最佳答案

您不能将引用绑定(bind)到 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 , 并根据需要在之前/之后分配。

live example

关于c++ - 在模板中将 char 转换为 int 引用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32872505/

相关文章:

c++ - 返回其自身类型的结构的结构属性

c++ - 重定向后重置 cout

html - 如何在 PUG 中使用包含文件内的 block ?

C++:非内联模板方法的放置

c++ - 如何制作像 R f<void(int)>(args...) 这样的模板函数

c++ - 调用导入函数的访问冲突

c++ - cpp 文件的 osx coretext header ,10.8 之前?

c++ - gcc中的初始化列表错误?

c++ - 层次结构中的反向可变参数模板参数

c++ - c++11 的 lambda 捕获初始化程序警告