键入从签名到无签名问题的转换

时间:2013-07-16 19:06:47

标签: 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(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或自定义实现进行编译。任何想法都非常感谢!

2 个答案:

答案 0 :(得分:3)

你在这里和那里错过了几个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到无符号指针。

答案 1 :(得分:1)

您忘记了typename

typename make_unsigned<T>::type obj;

在本网站上搜索“依赖名称”和“类型名称”,您将获得许多讨论此主题的主题。