gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 02:19:59 25 4
gpt4 key购买 nike

[更新: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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com