自包含,STL兼容的std :: vector实现 [英] Self-contained, STL-compatible implementation of std::vector

查看:238
本文介绍了自包含,STL兼容的std :: vector实现的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Visual Studio 2010和早期版本附带的 std :: vector 的实现有一个众所周知的特殊性: resize 方法具有以下签名(符合C ++ 03):

  void resize(size_type new_size,value_type value) ; 

,而不是大多数其他STL实现使用的符合C ++ 11的签名例如gcc的STL或STLport)在C ++ 11之前:

  void resize(size_type new_size,const value_type& value); 

第一个变体的问题是,在某些情况下,如果 value_type 具有对齐规范:

  struct __declspec(align(64))S {...}; 
std :: vector< S> v; //错误C2719:'_Val':与__declspec(align('64'))形式参数不会对齐

这是 已知问题没有满意的解决方法除了使用不同的实现 std :: vector



我正在寻找一个写得很好,兼容的实现 std :: vector 与MIT样式的许可证,我可以放入我的项目作为一个容器的对齐类型的首选。



我考虑从STLport或gcc的STL中提取它,但是完全符合标准,它们都有很多非平凡的依赖。



(我将非常满意 std :: vector 的一个合理子集的实现,只支持 push_back 清除容量 reserve resize swap / p>

任何想法?

解决方案 http://eigen.tuxfamily.org> Eigen 库似乎找到了一个很好的解决方法,存储过度对齐的类型(作为Stephan T. Lavavej将它们称为)转换为 std ::



它们的实现似乎不必要的复杂(检查源这里这里),但主要的想法是封装进入 std :: vector 和一个薄包装的类型:

  #include< vector> 

template< typename T>
struct wrapper:public T
{
wrapper(){}
wrapper(const T& rhs):T(rhs){}
};

struct __declspec(align(64))S
{
float x,y,z,w;
};

int main()
{
std :: vector<包装物> v; // OK,no C2719 error
return 0;
}

关于Eigen的实现,我必须承认我不太明白




  • 为什么需要 Eigen :: aligned_allocator_indirection

  • 为什么他们需要对 EIGEN_WORKAROUND_MSVC_STL_SUPPORT

  • 中的算术类型进行例外处理,为什么需要定义所有这些构造函数和运算符 Eigen :: workaround_msvc_stl_support

  • 或为什么需要重新定义 resize 在其 std :: vector 的部分专门化 Eigen :: aligned_allocator_indirection allocator ...



欢迎链接。关键是,这个技巧完美的(就我所知),我没有看到任何错误,除了可能从轻微的无礼。


The implementation of std::vector that ships with Visual Studio 2010 and earlier versions has a well known particularity: the resize method has the following signature (C++03-compliant):

void resize(size_type new_size, value_type value);

instead of the C++11-compliant signature that's been used by the majority of other STL implementations (like gcc's STL or STLport) long before C++11:

void resize(size_type new_size, const value_type& value);

The problem with the first variant is that, in some situations, it will fail to compile if value_type has an alignment specification:

struct __declspec(align(64)) S { ... };
std::vector<S> v;  // error C2719: '_Val': formal parameter with __declspec(align('64')) won't be aligned

This is a well known problem with no satisfactory workaround apart from using a different implementation of std::vector.

I'm looking for a well-written, well-tested, self-contained and STL-compatible implementation of std::vector with a MIT-style licence that I could drop into my project as a container of choice for aligned types.

I considered extracting it from STLport or gcc's STL but, being fully standard-compliant, they're both large with many non-trivial dependencies.

(I would be perfectly happy with an implementation of a reasonable subset of std::vector that would only support push_back, clear, capacity, size, reserve, resize, swap and array indexing.)

Any ideas?

解决方案

The guys behind the Eigen library seem to have found a nice workaround for the problem of storing "overly-aligned types" (as Stephan T. Lavavej call them) into a std::vector as implemented in Visual Studio's STL.

Their implementation seems unnecessary complicated (check the sources here and here) but the main idea is to encapsulate the type that goes into the std::vector with a thin wrapper:

#include <vector>

template <typename T>
struct wrapper : public T
{
    wrapper() {}
    wrapper(const T& rhs) : T(rhs) {}
};

struct __declspec(align(64)) S
{
    float x, y, z, w;
};

int main()
{
    std::vector< wrapper<S> > v;  // OK, no C2719 error
    return 0;
}

About the implementation in Eigen, I must admit I don't quite understand

  • why they need Eigen::aligned_allocator_indirection,
  • why they need to make an exception for arithmetic types in EIGEN_WORKAROUND_MSVC_STL_SUPPORT,
  • why they need to define all these constructors and operators in Eigen::workaround_msvc_stl_support,
  • or why they need to redefine resize in their partial specialization of std::vector for the Eigen::aligned_allocator_indirection allocator...

Clues welcome. The point is, this trick works perfectly (as far as I can tell) and I don't see anything wrong with it, apart maybe from the slight inelegance.

这篇关于自包含,STL兼容的std :: vector实现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆