C ++抑制自动初始化和销毁 [英] C++ Suppress Automatic Initialization and Destruction

查看:137
本文介绍了C ++抑制自动初始化和销毁的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何抑制类型的自动初始化和销毁​​?虽然很好, T buffer [100] 自动初始化 buffer 的所有元素,超出范围,这不是我想要的行为。

  #include< iostream> 

static int created = 0,
destroyed = 0;

struct S
{
S()
{
++ created;
}
〜S()
{
++ destroyed;
}
};

template< typename T,size_t KCount>
class fixed_vector
{
private:
T m_buffer [KCount];
public:
fixed_vector()
{
//某些方式来抑制m_buffer的自动初始化
}

〜fixed_vector
{
//某种方式来抑制m_buffer的自动销毁
}
};

int main()
{
{
fixed_vector< S,100> arr;
}

std :: cout<< Created:\t<<创建<< std :: endl;
std :: cout<< Destroyed:\t<<破坏< std :: endl;
return 0;
}

此程式的输出为:

 创建:100 
毁坏:100


$ b b

我希望它是:

 创建:0 
毁坏:0

我唯一的想法是让 m_buffer 一些简单构造和破坏类型像 char ,然后依靠 operator [] 为我包装指针数学,虽然这看起来像一个可怕的黑客解。另一个解决方案是使用 malloc free ,但是给出了我不想要的间接级别。 / p>




我想要的原因是因为我正在创建一个容器,我不想支付初始化开销我不会使用的东西。例如,如果我的 main 函数是:

  int main 
{
{
std :: vector< S> vec;
vec.reserve(50);
}

std :: cout<< Created:\t<<创建<< std :: endl;
std :: cout<< Destroyed:\t<<破坏< std :: endl;
return 0;
}

输出是正确的:

 创建:0 
销毁:0


解决方案

您可以将数组创建为 char s的数组,然后使用 placement new 在需要时创建元素。

 模板< typename T,size_t KCount> 
class Array
{
private:
char m_buffer [KCount * sizeof(T)]; // TODO确保它正确对齐

T操作符[](int i){
return reinterpret_cast< T&>(m_buffer [i * sizeof(T)]);
}






问题看起来你想要一个稀疏数组,这有时候以名称​​ map ; o)(当然性能特征是不同的...)

 模板< typename T,size_t KCount> 
class SparseArray {
std :: map< size_t,T& m_map;
public:
T& operator [](size_t i){
if(i> KCount)
throwout of bounds;
return m_map [i];
}


How does one suppress the automatic initialization and destruction of a type? While it is wonderful that T buffer[100] automatically initializes all the elements of buffer, and destroys them when they fall out of scope, this is not the behavior I want.

#include <iostream>

static int created   = 0,
           destroyed = 0;

struct S
{
    S()
    {
        ++created;
    }
    ~S()
    {
        ++destroyed;
    }
};

template <typename T, size_t KCount>
class fixed_vector
{
private:
    T m_buffer[KCount];
public:
    fixed_vector()
    {
        // some way to suppress the automatic initialization of m_buffer
    }

    ~fixed_vector()
    {
        // some way to suppress the automatic destruction of m_buffer
    }
};

int main()
{
    {
        fixed_vector<S, 100> arr;
    }

    std::cout << "Created:\t"   << created   << std::endl;
    std::cout << "Destroyed:\t" << destroyed << std::endl;
    return 0;
}

The output of this program is:

Created:    100
Destroyed:  100

I would like it to be:

Created:    0
Destroyed:  0

My only idea is to make m_buffer some trivially constructed and destructed type like char and then rely on operator[] to wrap the pointer math for me, although this seems like a horribly hacked solution. Another solution would be to use malloc and free, but that gives a level of indirection that I do not want.


The reason why I want this is because I am making a container and I do not want to pay for the initialization overhead of things that I will not use. For example, if my main function was:

int main()
{
    {
        std::vector<S> vec;
        vec.reserve(50);
    }

    std::cout << "Created:\t"   << created   << std::endl;
    std::cout << "Destroyed:\t" << destroyed << std::endl;
    return 0;
}

The output would be correct:

Created:    0
Destroyed:  0

解决方案

You can create the array as array of chars and then use placement new to create the elements when needed.

template <typename T, size_t KCount>
class Array
{
private:
    char m_buffer[KCount*sizeof(T)]; // TODO make sure it's aligned correctly

    T operator[](int i) {
        return reinterpret_cast<T&>(m_buffer[i*sizeof(T)]);
    }


After re-reading your question it seems that you want a sparse array, this sometimes goes by the name of map ;o) (of course the performance characteristics are different...)

template <typename T, size_t KCount>
class SparseArray {
    std::map<size_t, T> m_map;
public:
    T& operator[](size_t i) {
        if (i > KCount)
            throw "out of bounds";
        return m_map[i];
    }

这篇关于C ++抑制自动初始化和销毁的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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