我需要C ++数组类模板,它是固定大小的,基于堆栈的,并不需要默认的构造函数 [英] I need C++ array class template, which is fixed-size, stack-based and doesn't require default constructor

查看:125
本文介绍了我需要C ++数组类模板,它是固定大小的,基于堆栈的,并不需要默认的构造函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,我一直在寻找的boost ::数组,但它确实需要定义默认构造函数。
我想填补这一阵列数据的最佳方式,是通过一个的push_back(const的T&安培;)方法。调用比SIZE(在编译时已知)多次将导致断言或异常,具体取决于生成配置。这样,它总是会包含有意义的数据。
有谁知道高效,便携,可靠地实现这个概念的?

So, I've been looking at boost::array but it does require default constructor defined. I think the best way of filling this array with data, would be through a push_back(const T&) method. Calling it more times than SIZE (known at compile-time) would result in assert or exception, depending on build configuration. This way it would always contain meaningful data. Does anyone know efficient, portable, reliable implementation of this concept?

推荐答案

好吧,我本来以为会有人现在已经带来了答案,但它似乎没有,所以我们走。

Well, I would have thought that someone would have brought the answer now, however it seems not, so let's go.

你所希望的东西我自己的梦想:一个的boost :: optional_array< T,N>

What you are wishing for is something I have myself dreamed of: a boost::optional_array<T,N>.

有两种变体:


  • 第一:类似于的boost ::数组&LT;推动::可选&LT; T&GT,N方式&gt; ,即每个元素可能会或可能不会被置

  • 二:类似于的std ::矢量&lt; T&GT; (不知),这是所有开始元素设置,所有下面的人不在

  • First: similar to boost::array< boost::optional<T>, N >, that is each element may or may not be set.
  • Second: similar to a std::vector<T> (somehow), that is all beginning elements are set and all following ones are not.

由于previous问题/意见,看来你想第二个,但事情并没有真正因为都是不太一样。

Given the previous questions / comments, it seems you would like the second, but it doesn't really matter as both are quite alike.

template <typename T, size_t N>
class stack_vector
{
public:
  bool empty() const { return mSize == 0; }
  size_t size() const { return mSize; }
  size_t capacity() const { return N; }
  size_t max_size() const { return N; }

  T& operator[](size_t i) { return *(this->pfront() + i); }
  /// ...

private:
  T* pfront() const { return reinterpret_cast<T*>(&mStorage); }

  std::aligned_storage< N * sizeof(T), alignof(T) > mStorage;
  size_t mSize; // indicate how many elements are set, from the beginning
};

让我们专注于那些非常特殊的操作:

Let's focus on those very special operations:

template <typename T, size_t N>
void push_back(T const& t)
{
  new (this->pfront() + mSize) T(t); // in place construction
  ++mSize;
}

template <typename T, size_t N>
void clear()
{
  for (size_t i = 0; i != mSize; ++i)
  {
    (this->pfront() + i)->~T();
  }
  mSize = 0;
}

正如你可以看到,主要的困难是要记住的是:

As you can notice, the main difficulty is to remember that:


  • 如果没有元素已建有呢,你需要放置新+复印件建设,而不是分配。

  • 的元素变得过时(即会是最后一个元素之后)应妥善处理(即它们的析构函数被调用)。

有传统的STL容器许多操作可能会非常棘手实现。在矢量元素洗牌(由于插入擦除)可能是最粘滞的例子。

There are many operations on traditional STL container that may be tricky to implement. On a vector, element shuffling (due to insert or erase) are perhaps the most stricking examples.

另外请注意,用C ++ 0x中和初始化-名单矢量 GET emplace_back 来直接构造元素的地方,从而解除了复制构造的要求,可能是一个不错的福音依赖于你的情况。

Also note that with C++0x and initializer-lists vector get emplace_back to directly construct an element in place, thus lifting the CopyConstructible requirement, might be a nice boon dependent on your case.

这篇关于我需要C ++数组类模板,它是固定大小的,基于堆栈的,并不需要默认的构造函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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