Why does an empty vector call the value type's default constructor?

前端 未结 2 839
日久生厌
日久生厌 2021-01-07 17:19

Using g++, I observe that creating a vector of size zero calls the vector\'s parameterized object type\'s constructor once. It then is deleted. Why does this happen?

2条回答
  •  囚心锁ツ
    2021-01-07 17:43

    The actual constructor you are calling is (from cplusplus.com):

    explicit vector ( size_type n, const T& value= T(), const Allocator& = Allocator() );
    

    So even though you only specify the size, a new T object is created for second parameter, and will therefore also be destroyed at the conclusion of the constructor.

提交回复
热议问题