Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/131.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 从有符号到无符号的类型转换问题_C++_Templates_Types - Fatal编程技术网

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

C++ 从有符号到无符号的类型转换问题,c++,templates,types,C++,Templates,Types,我的函数如下所示: 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(v

我的函数如下所示:

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);
}
模板
无效myF(无效*a、T*b、T c)
{
make_unsigned::键入newC;
make_unsigned::type*newB=ptr_cast(b);
...
}
模板T ptr_铸件(无效*ptr)
{
返回静态_-cast(ptr);
}
它使用类型_traits类。它在VS2010中工作得很好,但是在我使用ARM编译器编译时失败了。编译器是v。5.02:

我得到:找不到文件类型\u特征。。。我还以为这是标准图书馆的一部分呢

我尝试了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

};
名称空间内部{
#定义MK_MAKEUNSIGNED(T,V)\
模板结构make_unsigned{\
公众:\
V型\
};
模板
结构make_未签名{
T型;
};
MK_MAKEUNSIGNED(sdata8,data8);
MK_MAKEUNSIGNED(sdata16,data16);
MK_MAKEUNSIGNED(sdata32,data32);
MK_MAKEUNSIGNED(sdata64,data64);
#未定义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);
    ...
}
模板
无效myF(无效*a、T*b、T c)
{
内部::make_unsigned::键入newC;
内部::make_unsigned::type*newB=ptr_cast(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                                                          
                                                                                     ^
internal::make_unsigned::type newC#276 name后跟“::”必须是类或命名空间名称
^  
内部::make_unsigned::键入newC#282全局作用域没有“类型”
^ 
内部::make_unsigned::键入newC#65:应为“;”
^
内部::make_unsigned::type*newB=static_ptr(b)#276 name后跟“::”必须是类或命名空间名称
^
内部::make_unsigned::type*newB=static_ptr(b)#282全局作用域没有“类型”
^
内部::make_unsigned::type*newB=static_ptr(b)#20标识符“newB”未定义
^
内部::make_unsigned::type*newB=static_ptr(b)#276 name后跟“::”必须是类或命名空间名称
^
内部::make_unsigned::type*newB=static_ptr(b)#276 name后跟“::”必须是类或命名空间名称
^
内部::make_unsigned::type*newB=static_ptr(b)#29期待一个表情
^

因此,我似乎无法使用type_traits或自定义实现来编译它。非常感谢您的任何想法

您忘记了
类型名称

typename make_unsigned<T>::type obj;
typename make_unsigned::type obj;

在这个网站上搜索“相依名称”和“类型名称”,你会得到许多讨论这个问题的主题。

你在这里和那里遗漏了几个
typename
s

typename internal::make_unsigned<T>::type newC;
// ^^^^^

因为你不能将有符号的指针转换成无符号的指针。

多年来一直是标准的,是的,它有
std::make_unsigned
,你可以专门化。当然,如果“years”指的是2
type_traits
是TR1的一部分,但直到2011年才被纳入语言标准。谢谢你的解释。更正:我没有直接使用静态投影。相反,我编写了一个函数,它接受一个void*并将其强制转换为模板类型。我没有正确地表明这一点。OP已更新以反映这一点。@radensb:从技术上讲,
ptr\u cast
仍然是未定义的行为。您不允许将
static\u cast
a
void*
转换为从中获取
void*
的原始类型以外的类型。为此,您应该使用
重新解释\u cast
(这仍然是未指定的,但至少明确了您正在做的事情)
typename make_unsigned<T>::type* newB = static_cast<make_unsigned<T>::type*>(b);