如何在C++中剥离一个类型的所有信息
How do i strip all info of a type in C++?
假设我有一个static const int*const
或const int&
。我需要它的结果int
,以便在模板中使用。如何去掉所有数据,得到一个简单的int
?
decay
和remove_pointer
的组合:
#include <type_traits>
typedef typename std::remove_pointer<typename std::decay<T>::type>::type TStrip;
这将完成任务:
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;
};
请参阅http://ideone.com/ZNFkm
相关文章:
- 我想将一个对T类型的非常量左值引用绑定到一个T类型的临时值
- 构造函数正在调用一个使用当前类类型的函数
- 引用一个已擦除类型(void*)的指针
- 在一个模板函数中,若输入的类型是enum类,我该如何使用std::underlying_type
- visual是否可以在c++中创建一个接收无限数量相同类型(或至少相当数量)参数的函数
- 在c++中为double类型的数组创建一个unique_ptr
- 当基类是依赖类型时,这是一个缺陷吗
- int数据类型的指针指向的是什么,如果是一个类的私有数据成员,我们创建了该类的两个对象?
- 表达式 SFINAE:如何根据类型是否包含具有一个或多个参数的函数来选择模板版本
- 检查一个类型是否直接派生自"enable if"上下文中的另一个类型(是其子类型)
- 给定一个类型,如何派生一个泛型更广泛的类型(例如,用于溢出安全求和)?
- 有没有办法一次声明相同类型的多个对象,并通过一个表达式立即使用相同的右值初始化它们?
- 给定一个C++嵌套的私有结构类型,是否有从文件范围静态函数访问它的策略
- C++将一个指针分配给另一个指针时执行的类型检查
- 模板函数如何处理可能共享一个交集的多个类型名称?
- 是否可以在C++中有一个"generic"模板参数,该参数可以是非类型模板参数或类型?
- 取消引用结束指针到数组类型的一个
- 为什么转换函数声明不需要至少一个定义类型说明符
- 如何包装一个函数以适应另一个函数的所需类型
- 使用C++生成泛型类型-一个具有共享实现的模板