如何在Visual c++ 9中使用类型特征检测按位移动类型?
How do I detect bitwise-moveable types using type traits in Visual C++ 9?
我有一个用Visual c++ 2008编译的std::vector
类。在这个类中有一个部分是移动存储元素的——要么重新分配主体,要么进行插入/部分擦除。现在,有些类型可以直接使用memmove()
d,而其他类型则需要在新位置复制构造,然后在原始位置销毁。
目前有一个模板化函数,实现了"复制构造,然后销毁",并且每种类型都可以是memmove()
d。我想使用Visual c++ 2008支持类型特征来简化该任务。
我想要的是为每个POD自动选择memmove()
。我不介意使用模板函数移动一些可以是 memmove()
d的类型,但我不能容忍相反的情况-当不能是 memmove()
d的数据类型是 memmove()
d时。
看起来像__is_pod()
固有的,但MSDN说它返回false
的内置类型。所以我想我还需要使用__is_class()
首先过滤掉非类。
所以我想这样做就可以了:
if( !__is_class(T) ) { // not a struct and not a class - built-in type
//memmove()
} else if( __is_pod(T) ) { // is a struct or a class and is a POD
//memmove()
} else { // is a struct or a class and is not a POD
//DefaultMoveViaCopy()
}
我的解决方案可以吗?
使用boost的enable_if:
template <typename T, class Enabler = void>
struct SmartCopy
{
static void apply(const T& obj, T* dest)
{
// Generic copy...
}
}
template <typename T>
struct SmartCopy<T, typename boost::enable_if_c<__is_pod(T) || !__is_class(T)>::type >
{
static void apply(const T& obj, T* dest)
{
// memmove version...
}
}
template <typename T>
void smart_move(const T& obj, T* dest)
{
SmartCopy<T>::apply(obj, dest);
}
相关文章:
- 根据C++标准的定义实现"is_similar"类型特征
- C++类型特征,以查看是否可以<uint32_t>对类型"K"的任何变量调用"static_cast(k)"
- C ++类型特征:确保子类实现方法
- 根据类型特征更改函数定义?
- 如何使用类型特征将函数的通用引用参数限制为 r 值引用?
- 在C++中创建新的类型特征
- 如何构造一个类型特征,可以判断一个类型的私有方法是否可以在另一个类型的构造函数中调用?
- std::begin-类型特征中未考虑用户定义的重载
- 在类型特征修改后,无法限定类型
- 使用类型特征的部分类专用化
- 为什么不调用预期的函数?我是否对类型特征的理解不正确?
- 了解类型特征的体系结构
- 为什么使用某些类型特征的模板类型会导致模板扣除失败
- 有了C++类型特征,有没有办法避免铸造
- 哪种类型特征表明该类型是可分配的?(元组,对)
- 一种类型特征,标识哪个类提供通过重载解析选择的函数
- 如何使用类型特征正确推断引用的参数
- 如何通过扩展以下类型特征来删除 decltype(&MyClass::funct) 部分?
- 如何在Visual c++ 9中使用类型特征检测按位移动类型?
- 用于检测模板函数的 Sfinae 类型特征不适用于 std::forward