在模板中将char转换为int引用
Casting char to int reference in a template
[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&}’
问题:
- 我怎样才能使它工作?
- 如何避免为左值和右值引用重写模板专门化?
不能绑定int
对char
的引用。
如果你想读取另一种类型的内容,做更多的工作:
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
将不允许将char
或char&
转换为int&
。这就是在下面的调用中将要发生的事情:
f1>>type_transform(variable);
如果variable
是char
的类型
相关文章:
- 从"int*"强制转换为"unsigned int"会丢失精度错误
- 是否可以从int转换为enum类类型
- 如何在C++中将一个无符号的 int 转换为两个无符号的短裤?
- 功能样式转换从 'int' 到 'ItemType' 的匹配转换
- 将 int 数组转换为 std::vector<int*>
- 如何转换 int 变量并附加到常量 wchar_t*
- 类型转换<int>的舍入问题
- 在Objective-C中调用sqlite3数据库时,如何调用和转换int/NSNumber
- 转换 int -> 无符号长 长 是否由标准定义
- 简单的 int 到字符串转换..int to const char* 无效
- 如何将转换 int 除法键入为浮点数
- C++类型转换 int * 到类
- 预期的类型说明符,并且无法在初始化时转换“int*”
- 转换int并将其与char*连接
- 字符串流在转换int型时添加新行
- 转换int为字符串/char c++ /Arduino
- c# P-Invoke:如何转换INT fun(BYTE *bStream, UINT16 *nCount, const
- 类型转换(int**)和int(*)的区别
- 从"int"到"nodeType"的无效转换<int>
- 从无符号int构造对强制转换int的引用