Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.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++中的有符号整型的无符号等价(相同大小)?我的思路是: template<typename T> struct get_unsigned { }; template<> struct get_unsigned<int> { typedef unsigned int type; }; ... template<typename T> void myfunc(T val) { get_unsigned<T>::type u = std::abs(val); ... } 模板 结构get_unsigned{}; 模板 结构get_unsigned{ typedef无符号整数类型; }; ... 模板 无效myfunc(T val){ get_unsigned::type u=std::abs(val); ... }_C++_Templates - Fatal编程技术网

如何在C++;? 有没有方法得到C++中的有符号整型的无符号等价(相同大小)?我的思路是: template<typename T> struct get_unsigned { }; template<> struct get_unsigned<int> { typedef unsigned int type; }; ... template<typename T> void myfunc(T val) { get_unsigned<T>::type u = std::abs(val); ... } 模板 结构get_unsigned{}; 模板 结构get_unsigned{ typedef无符号整数类型; }; ... 模板 无效myfunc(T val){ get_unsigned::type u=std::abs(val); ... }

如何在C++;? 有没有方法得到C++中的有符号整型的无符号等价(相同大小)?我的思路是: template<typename T> struct get_unsigned { }; template<> struct get_unsigned<int> { typedef unsigned int type; }; ... template<typename T> void myfunc(T val) { get_unsigned<T>::type u = std::abs(val); ... } 模板 结构get_unsigned{}; 模板 结构get_unsigned{ typedef无符号整数类型; }; ... 模板 无效myfunc(T val){ get_unsigned::type u=std::abs(val); ... },c++,templates,C++,Templates,我正在标准库或Boost中寻找一个现有的解决方案,我不想推出自己的解决方案,除非有几行。Boost.TypeTraits有: 类型:如果T是无符号整数类型,则与T的类型相同;如果T是有符号整数类型,则为相应的无符号类型。否则,如果T是枚举或字符类型(char或wchar\u T),则为与T宽度相同的无符号整数类型 如果T有任何cv限定符,那么这些限定符也会出现在结果类型上 远远不止几行。正如下划线d在“James McNellis”答案中所评论的,现在可以使用标准的std::make_unsig

我正在标准库或Boost中寻找一个现有的解决方案,我不想推出自己的解决方案,除非有几行。

Boost.TypeTraits有:

类型:如果T是无符号整数类型,则与T的类型相同;如果T是有符号整数类型,则为相应的无符号类型。否则,如果T是枚举或字符类型(char或wchar\u T),则为与T宽度相同的无符号整数类型

如果T有任何cv限定符,那么这些限定符也会出现在结果类型上


远远不止几行。

正如下划线d在“James McNellis”答案中所评论的,现在可以使用标准的
std::make_unsigned
函数:


如果您不想使用boost来实现这一点,那就太好了。

这是在C++11中添加到stdlib的,可以预见,
std::make_unsigned