初始化自定义数组类,而不创建副本 [英] Initialize custom array class without creating copies

查看:127
本文介绍了初始化自定义数组类,而不创建副本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写了一个自定义数组容器,因为大多数C ++库包括 std :: array std :: iterator 不适用于目标(嵌入式)。模板类大多按预期工作。

然而,我没有设置一个构造函数,它可以初始化整个数组而不创建副本。我试过几个构造函数,包括通过引用。显示的将创建一个副本,如果传递的数组的大小不匹配我的类中的一个问题。



环境中支持高达C ++ 11的语言功能,但没有std库可用。因此,无法使用类似问题的答案。 / p>

模板类的源代码:

  typename sizetype,sizetype SIZE> 
class mArray
{
private:
T _elements [SIZE]; ///< T
的SIZE元素数组static_assert(SIZE> 0,Size must be> 0); //拒绝空数组
public:
mArray(){};
mArray(T initArr [SIZE])
{
sizetype index = 0;
for(auto element:_elements)
{
_elements [index] = initArr [index]; //
++ index;
}
}
T * begin()
{
return& _elements [0]; //指向第一个_element
}
T * end()
{
return& _elements [SIZE]; //指针后面的_elements
}
T front()
{
return _elements [0]; //返回_elements的第一个元素
}
T back()
{
return _elements [SIZE-1]; //返回_elements的最后一个元素
}
T在(sizetype pos)
{
static_assert(pos> SIZE,pos必须< SIZE);
return _elements [pos];
}
T操作符[](sizetype pos)
{
return _elements [pos];
}
T * data()
{
return * _elements;
}
sizetype size()
{
return SIZE;
}
};

如何改进构造函数以避免创建副本?

解决方案

这样做的方式涉及聚合初始化。如果你试图获取一个数组并从中复制,那当然会需要副本。然而,如果你被传递一个初始化列表,那么你只需要使元素 public,并与 std :: array 语法:

  template< typename T,typename sizetype,sizetype SIZE> 
class mArray {
public:
T elements_ [SIZE];
static_assert(SIZE> 0,Size must be> 0);
T operator [](sizetype pos){//你确定吗?
return element_ [pos];
}

//其他成员函数
};

int main(){
mArray< int,std :: size_t,10> arr {{1,3,5,7}};
}


I wrote a custom array container, as most C++ libraries including std::array and std::iterator are not available for the target (embedded). The template class is mostly working as intended.
However I did not manage to set up a constructor, which can initialize the whole array without creating copies. I tried several constructors including passing by reference. The one shown will create a copy and has issues if the size of the passed array does not match the one of my class.

Language features up to C++11 are supported in the environment, but no std libraries are available. Therefore the answers to similar questions could not be used.

The sourcecode of the template class:

template<typename T, typename sizetype, sizetype SIZE>
class mArray
{
private:
    T _elements[SIZE]; ///< Array of SIZE elements of Type T
    static_assert( SIZE > 0, "Size must be > 0"); // Discourage empty array
public:
    mArray(){};
    mArray(T initArr[SIZE])
    {
        sizetype index = 0;
        for (auto element : _elements)
        {
            _elements[index] = initArr[index]; //
            ++index;
        }
    }
    T* begin()
    {
        return &_elements[0]; // Pointer to first _element
    }
    T* end()
    {
        return &_elements[SIZE]; // Pointer behind last _elements
    }
    T front()
    {
        return _elements[0]; // Return first element of _elements
    }
    T back()
    {
        return _elements[SIZE-1]; // Return last element of _elements
    }
    T at(sizetype pos)
    {
        static_assert( pos > SIZE, "pos must be < SIZE");
        return _elements[pos];
    }
    T operator[](sizetype pos)
    {
        return _elements[pos];
    }
    T* data()
    {
        return *_elements;
    }
    sizetype size()
    {
        return SIZE;
    }
};

How can I improve the constructor to avoid creating copies?

解决方案

The way this is done involves aggregate initialization. If you are trying to take an array and copy from it, that will of course require copies. However, if you are being passed an initializer list then you just need to make elements public and go with the std::array syntax:

template<typename T, typename sizetype, sizetype SIZE>
class mArray {
 public:
  T elements_[SIZE];
  static_assert( SIZE > 0, "Size must be > 0");
  T operator[](sizetype pos) { // you sure about this?
    return elements_[pos];
  }

  // other member functions
};

int main() {
  mArray<int, std::size_t, 10> arr{{1, 3, 5, 7}};
}

这篇关于初始化自定义数组类,而不创建副本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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