从有符号问题到无符号问题的类型转换
Type conversion from signed to unsigned problems
我有一个如下函数:
template<typename T>
void myF(void* a, T* b, T c)
{
make_unsigned<T>::type newC;
make_unsigned<T>::type* newB = ptr_cast<make_unsigned<T>::type*>(b);
...
}
template <typename T> T ptr_cast(void* ptr)
{
return static_cast<T>(ptr);
}
这是使用type_traits类。它在VS 2010中工作得很好,但是当我使用ARM编译器编译时它会失败。编译器为 v. 5.02:http://infocenter.arm.com/help/index.jsp?topic=/com.arm.doc.subset.swdev.ds5/index.html
我得到:找不到文件type_traits...我以为它在这一点上是标准库的一部分?
我尝试了make_unsigned的自定义实现:
namespace internal {
#define MK_MAKEUNSIGNED(T,V)
template<> struct make_unsigned<T> {
public:
typedef V type;
};
template<typename T>
struct make_unsigned {
typedef T type;
};
MK_MAKEUNSIGNED(sdata8, data8);
MK_MAKEUNSIGNED(sdata16, data16);
MK_MAKEUNSIGNED(sdata32, data32);
MK_MAKEUNSIGNED(sdata64, data64);
#undef MK_MAKEUNSIGNED
};
并修改为:
template<typename T>
void myF(void* a, T* b, T c)
{
internal::make_unsigned<T>::type newC;
internal::make_unsigned<T>::type* newB = ptr_cast<internal::make_unsigned<T>::type*>(b);
...
}
同样,它在VS 2010中工作,但ARM编译器给出以下错误:
internal::make_unsigned<T>::type newC; #276 name followed by "::" must be a class or namespace name
^
internal::make_unsigned<T>::type newC; #282 the global scope has no "type"
^
internal::make_unsigned<T>::type newC; #65: expected a ';'
^
internal::make_unsigned<T>::type* newB = static_ptr<internal::make_unsigned<T>::type*>(b); #276 name followed by "::" must be a class or namespace name
^
internal::make_unsigned<T>::type* newB = static_ptr<internal::make_unsigned<T>::type*>(b); #282 the global scope has no "type"
^
internal::make_unsigned<T>::type* newB = static_ptr<internal::make_unsigned<T>::type*>(b); #20 identifier 'newB' nis undefined
^
internal::make_unsigned<T>::type* newB = static_ptr<internal::make_unsigned<T>::type*>(b); #276 name followed by "::" must be a class or namespace name
^
internal::make_unsigned<T>::type* newB = static_ptr<internal::make_unsigned<T>::type*>(b); #276 name followed by "::" must be a class or namespace name
^
internal::make_unsigned<T>::type* newB = static_ptr<internal::make_unsigned<T>::type*>(b); #29 expected an expression
^
所以我似乎无法使用type_traits或自定义实现来编译它。任何想法都非常感谢!
你在这里和那里错过了几个typename
...
typename internal::make_unsigned<T>::type newC;
// ^^^^^
基本上,internal::make_unsigned<T>::type
是一个依赖名称,除非您使用 typename
指示编译器,否则假定它不是一个类型。VS过于宽松,让我们过去吧。
除此之外,这不会编译:
typename make_unsigned<T>::type* newB = static_cast<make_unsigned<T>::type*>(b);
因为您无法从指针static_cast
到已签名的指针到无符号的指针。
你忘了typename
:
typename make_unsigned<T>::type obj;
在此站点上搜索"依赖名称"和"类型名称",您将获得许多讨论此主题的主题。
相关文章:
- 有关插入适配器的错误。[错误]请求从 'back_insert_iterator<vector<>>' 类型转换为非标量类型
- 处理小于cpu数据总线的数据类型.(c++转换为机器代码)
- C++中的双指针类型转换
- 类型转换问题
- 拆分 pybind11 模块和自动类型转换问题
- 类型转换问题:返回为整数而不是浮点/类型
- 关于使用类型转换优化除法代码的问题
- C 中某些类型转换的问题
- C 复合分配和类型转换问题
- 类型转换<int>的舍入问题
- 将数据类型从C#封送至C++的类型转换问题
- 类型转换问题:字符数组的元素转换为整数变量
- 将标量乘以boost.units.quantity(自动类型转换问题?)
- 使用createprocess()时出现类型转换问题
- 垫子类型转换和访问单个像素时出现问题
- Clang 问题:构造时隐式类型转换
- 从有符号问题到无符号问题的类型转换
- C++类型转换问题
- 类型转换问题
- C++中模板类的类型转换问题