防止对类中的数组调用默认构造函数 [英] Prevent calls to default constructor for an array inside class

查看:163
本文介绍了防止对类中的数组调用默认构造函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当写一个偏移数组类(你的idxs从说说100到1000,所以你创建类,考虑到这一点,而不浪费数组中的前100个插槽)我碰到一个问题。

如何初始化一个有C数组元素的类(问题是T does not有def构造函数)。基本上我想要数组是完全未启动。示例:

  class MyClass 
{
MyClass(int i)
{

}
};
template< typename T,size_t n,size_t offset>
struct offsetedIdxArray
{
T data [n]; //错误行:error C2512:'MyClass':没有适当的默认构造函数
offsetedIdxArray()
{

}
T& operator [](size_t pos)
{
return data [(pos-offset)];
}

};

用法:

 code> offsetedIdxArray< MyClass,1024,offset> ; 

使用def构造函数不是选项,因为我使用的类实际上是库类。



* 编辑:* 与这里描述的问题无关,但结果是我的珍贵的库类没有复制ctor, ,所以我不得不使用unique_ptr的向量。

解决方案

要获取存储空间的静态大小的未初始化部分,可以使用对齐存储的无类型缓冲区, C ++ 11中的code> std :: aligned_storage< sizeof(T [n]),alignof(T)> :: type char [sizeof(T [n])+ something] 并进行手动更正以进行对齐,或使用 char [sizeof(T [n] ] 和编译器扩展来指定对齐方式)。



这意味着对构造函数使用placement new和销毁显式析构函数。它是由你跟踪哪些部分的存储具有对象(因此需要销毁)和什么部分没有对象(不能有析构函数调用)。当客户端请求一个没有被初始化的元素(在它应该在的地方没有对象)时,这也取决于你。



一个替代方法是使用 boost :: optional 的数组,然后你不必关心销毁,并且可以简单地为它们各自的索引分配新的元素。 / p>

while writing a offset array class(your idxs go from lets say 100 to 1000, so you create class that takes that into account without wasting first 100 slots in the array) I ran into a problem.
How to initialize a class that has an C array of elements(problem is that T doesnt have def constructor). Basically I want the array to be totally uninitiated. Example:

class MyClass
{
    MyClass(int i)
    {

    }
};
template <typename T, size_t n, size_t offset>
struct offsetedIdxArray
{
    T data[n];// error line : error C2512: 'MyClass' : no appropriate default constructor available
    offsetedIdxArray()
    {

    }
    T& operator [](size_t pos)
    {
        return data[(pos-offset)];
    }

};

usage:

offsetedIdxArray<MyClass, 1024,offset> oia;

Making def constructor is not the option because class I use is in fact library class.

*EDIT: * not related to problem described here, but it turned out that my precious library class doesnt have copy ctor, just move ctor, so I had to use vector of unique_ptr.

解决方案

To get a statically-sized uninitialized portion of storage, you can use an "untyped" buffer of aligned storage, like std::aligned_storage<sizeof(T[n]), alignof(T)>::type in C++11 (in C++03 you need to use a char[sizeof(T[n])+something] and do manual corrections for alignment, or use a char[sizeof(T[n])] and compiler extensions to specify alignment).

That means using placement new for constructors, and explicit destructor calls for destruction. It is up to you to track what parts of that storage have objects (and thus needs destruction) and what parts don't have objects (and can't have destructors called on). It is also up to you to cater for when the client requests an element that isn't initialized at all (there's no object at the place it's supposed to be).

An alternative is to use an array of boost::optionals, and then you don't have to care about destruction and can simply assign new elements to their respective index.

这篇关于防止对类中的数组调用默认构造函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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