开发者

Does the vector.resize() method calls the default elements constructors when resizing?

开发者 https://www.devze.com 2023-04-04 14:17 出处:网络
I am trying the following code: struct _Struct2 { void*ptr; double dval; }; struct _Struct { float fval; intival;

I am trying the following code:

struct _Struct2
{
    void   *ptr;
    double dval;
};

struct _Struct
{
    float fval;
    int   ival;
    std::vector<_Struct2>   data;
};

std::vector<_Struct>    vec;


int main()
{
    vec.resize( 9 );
    for ( int i = 0; i < vec.size(); i++ )
    {
        _Struct &elem = vec[i];
        int     len = elem.data.size(); // elem.data is [0]()
    }
}

The resize(9) should allocate 9 elements of type _Struct. And, in fact it works. But every element of type _Struct is not initialized, especially the data element, which is another std::vector. I would like it to be initialized to the empty std::vector. Have to do that manually? I thought开发者_如何转开发 that the resize() method would have called the default constructor of every _Struct element. Thx

Ps. The names of the structs used here are just the first things that come to my mind. This is just an example. My Visual Studio tells me that elem.data correspond, in the debug view, to [0]().

Ps. Forget the [0]().


No it doesn't call default element constructor. std::vector never calls default constructors internally (it does in C++11, but not in earlier versions of the specification).

The full signature for vector::resize looks as follows

void resize(size_type sz, T c = T());

I.e. it has a second parameter (with default argument value). That second parameter is then used as a "source" object to initialize new elements by copy-constructor.

In other words, your call to resize is actually equivalent to

vec.resize( 9, _Struct() );

meaning that it is you who called the default constructor when you supplied that "source" object to vector::resize, even though you didn't notice that.

But every element of type _Struct is not initialized, especially the data element, which is another std::vector.

Huh? "Not initialized"? I don't know what that is supposed to mean, considering that in your sample code every new element created by resize is perfectly initialized as described above: it is copy-initialized from a _Struct() element you implicitly supplied to resize as the second argument. Each _Struct::fval and _Struct::ival is zero, and each _Struct::data is an empty vector.

(In the original C++98 _Struct::fval and _Struct::ival will remain uninitialized, since pre-TC1 C++98 didn't support value-initialization. But _Struct::data will be initialized to an empty vector even in the original C++98).

I would like it to be initialized to the empty std::vector.

Every _Struct::data vector is already initialized as an empty vector. What made you believe that it isn't?

P.S. Names that begin with _ followed by an uppercase letter are reserved by the implementation. You are not allowed to use them.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号