gpt4 book ai didi

c++ - 从有符号到无符号问题的类型转换

转载 作者:太空狗 更新时间:2023-10-29 21:01:39 25 4
gpt4 key购买 nike

我有一个类似下面的函数:

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从有符号指针到无符号指针。

关于c++ - 从有符号到无符号问题的类型转换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17684972/

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