C++ 如何在C++;?
假设我有一个C++ 如何在C++;?,c++,types,C++,Types,假设我有一个静态常量int*const或一个常量int&。我需要它的结果int,以便在模板中使用。如何剥离所有数据并只获取一个普通的int?这将实现以下技巧: template <typename T> struct strip { typedef T type; }; template <typename T> struct strip<const T&> { typedef T type; }; template <typenam
静态常量int*const
或一个常量int&
。我需要它的结果int
,以便在模板中使用。如何剥离所有数据并只获取一个普通的int
?这将实现以下技巧:
template <typename T>
struct strip
{
typedef T type;
};
template <typename T>
struct strip<const T&>
{
typedef T type;
};
template <typename T>
struct strip<const T* const>
{
typedef T type;
};
模板
结构条
{
T型;
};
样板
结构条
{
T型;
};
样板
结构条
{
T型;
};
请参见这将实现以下功能:
template <typename T>
struct strip
{
typedef T type;
};
template <typename T>
struct strip<const T&>
{
typedef T type;
};
template <typename T>
struct strip<const T* const>
{
typedef T type;
};
模板
结构条
{
T型;
};
样板
结构条
{
T型;
};
样板
结构条
{
T型;
};
请参阅衰减
和移除指针的组合:
#include <type_traits>
typedef typename std::remove_pointer<typename std::decay<T>::type>::type TStrip;
#包括
typedef typename std::remove_pointer::type TStrip;
衰减和移除指针的组合
:
#include <type_traits>
typedef typename std::remove_pointer<typename std::decay<T>::type>::type TStrip;
#包括
typedef typename std::remove_pointer::type TStrip;
你的意思是如何进行类型转换?我同意,类型转换应该足够(int)(*const\u ptr)
或将数据复制到新变量int tmp=(*const\u ptr)
(这确保你不会重写你的变量)@John3136:No.这样我就可以写strip::type a
,当t与我的问题类似时,你的意思是如何进行类型转换?我同意,类型转换应该足够(int)(*const_ptr)
或者将数据复制到新变量int tmp=(*const_ptr)
(这确保您不会重写变量)@John3136:No。这样我就可以用int来写strip::键入a
,当t与我的问题中的类似时,看起来不错。我认为它不会那么简单(如果我加入volatile会发生什么情况)是否没有我可以使用的标准类?@acidzombie24 volatile或static都没有问题(它们不影响类型),现在这是一个很好的答案,+1:)完美(我想)。为什么只有我一个人投了更高的票。。。。很好的答案和很好的例子看起来不错。我认为它不会那么简单(如果我加入volatile会发生什么情况)是否没有我可以使用的标准类?@acidzombie24 volatile或static都没有问题(它们不影响类型),现在这是一个很好的答案,+1:)完美(我想)。为什么只有我一个人投了更高的票。。。。哦,好的答案和很好的例子这个答案看起来可能更好。我需要在本周晚些时候进行测试。这个答案看起来可能更好。我需要在本周晚些时候进行测试。