自成一体,STL兼容实施的std ::矢量 [英] Self-contained, STL-compatible implementation of std::vector
问题描述
的的std ::矢量
的实施随Visual Studio 2010和更早版本有一个众所周知的特殊性:在调整
方法具有以下特征(C ++ 03标准):
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);
,而不是一个已经使用的其他大多数STL实现(如海湾合作委员会的STL和STLport的)的C ++ 11兼容的签名很久以前C ++ 11:
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);
与第一变体的问题在于,在某些情况下,它无法编译如果 value_type的
有定位规范
struct __declspec(align(64)) S { ... };
std::vector<S> v; // error C2719: '_Val': formal parameter with __declspec(align('64')) won't be aligned
这是一个以及 <一href="http://thetweaker.word$p$pss.com/2010/08/15/stdvector-of-aligned-elements-revisited/">known问题没有满意的解决方法除了使用不同的实施的std ::矢量
。
This is a well known problem with no satisfactory workaround apart from using a different implementation of std::vector
.
我正在寻找一个写得很好的,经过严格测试的,独立的,STL兼容实施的std ::矢量
与MIT-的风格的许可证,我可以拖放到我的项目作为首选对准类型的容器。
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.
我认为从STLport的还是gcc的STL中提取,但,是完全符合标准的,他们都大了许多不平凡的依赖关系。
I considered extracting it from STLport or gcc's STL but, being fully standard-compliant, they're both large with many non-trivial dependencies.
(我将非常乐意与的std ::向量的一个合理的子集的实施
,将只支持的push_back
,明确
,容量
,尺寸
,储备
,调整
,交换
和数组下标。)
(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.)
任何想法?
推荐答案
在征库似乎已经找到了一个不错的解决方法用于存储过度对齐类型的问题(<一href="https://connect.microsoft.com/VisualStudio/feedback/details/721286/vc-std-vector-does-not-support-data-alignment">as斯蒂芬T. Lavavej叫他们)成的std ::矢量
在Visual Studio中的STL实现。
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.
它们的实现似乎没有必要复杂(查看源这里和这里),但主要的想法是封装进入类型在的std ::矢量
薄薄的包装:
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
- 为什么他们需要
征:: aligned_allocator_indirection
, - 为什么他们需要作出例外算术类型
EIGEN_WORKAROUND_MSVC_STL_SUPPORT
, - 为什么他们需要定义所有这些构造和运营商
征:: workaround_msvc_stl_support
, - 为什么他们需要重新定义
调整
在的std ::向量的部分特
为征:: aligned_allocator_indirection
分配器...
- 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 ofstd::vector
for theEigen::aligned_allocator_indirection
allocator...
线索欢迎。问题是,这一招完美的作品(据我可以告诉),我看不出有什么不对的地方,除了可能从轻微的inelegance。
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 ::矢量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!