简体   繁体   English

结合std :: vector Default和Fill Constructor

[英]Combining std::vector Default and Fill Constructor

I had a fairly quick question. 我有一个相当快的问题。 The std::vector provides the following two constructors: std :: vector提供以下两个构造函数:

explicit vector( const Allocator& alloc = Allocator());      // default constructor

explicit vector( size_type count,                            // fill constructor
             const T& value = T(),
             const Allocator& alloc = Allocator());

Is there any particular reason the default constructor is not implemented with a default value of 0 for the first parameter in the fill constructor? 对于填充构造函数中的第一个参数,是否有任何特殊原因未使用默认值0实现默认构造函数? I can imagine there must be a reason but I can't immediately see one. 我可以想象必须有一个理由,但我不能马上看到一个。

Because you then can't pass just an allocator , without providing the count or default element (aka value ) as well? 因为你不能只传递一个allocator ,而不提供count或默认元素(也就是value )?

Putting count to 0 will result in ambiguity error. count置于0将导致歧义错误。

Would be much more simpler if C++ had named params like Python. 如果C ++像Python一样命名params会更简单。 Boost has such a library , but again that incurs some runtime overhead :( (can't remember how much right now) I often used this Lib in testing, but not where the performance is important... Boost有这样一个库 ,但同样会引起一些运行时开销:((现在记不清多少)我经常在测试中使用这个Lib,但不是性能很重要的地方......

The reason is that the constructors place different requirements on the type contained in the vector. 原因是构造函数对向量中包含的类型提出了不同的要求。 To use the second one, the type must copy-constructible, and if you use the default argument for value , it must be default-constructible too. 要使用第二个类型,类型必须是可复制的,如果使用value的默认参数,它也必须是default-constructible。 The first constructor places no such requirements on the contained type. 第一个构造函数对包含的类型没有这样的要求。

Note that the constructors you're showing in the question only existed until C++11. 请注意,您在问题中显示的构造函数仅存在于C ++ 11之前。 There, it was sufficient to differentiate those two scenarios (since any type stored in a std::vector had to be copy-constructible). 在那里,区分这两个场景就足够了(因为存储在std::vector中的任何类型都必须是可复制构造的)。 C++11 introduced move semantics, and the second constructor was split further: C ++ 11引入了移动语义,第二个构造函数进一步拆分:

explicit vector(size_type count);

vector(
  size_type count,
  const T& value,
  const Allocator& alloc = Allocator()
);

That's because std::vector no longer requires its contained types to be copy-constructibe; 那是因为std::vector不再需要它包含的类型是copy-constructibe; move-constructibility is enough. 移动构造性就足够了。 Therefore the count-only constructor requires default constructibility (but not copy constructibility), and the count + prototype constructor requires copy constructibility (but not default constructibility). 因此,仅计数构造函数需要默认的可构造性(但不是复制构造性),而count + prototype构造函数需要复制可构造性(但不是默认的构造性)​​。


The evolution of std::vector constructors is really quite complex. std::vector构造函数的发展非常复杂。 See their page on cppreference to see how much they've evolved. 查看他们在cppreference的页面,了解他们的进化程度。 That evolution includes adding an optional allocator parameter to the count-only constructor in C++14, which was (I assume) mistakenly omitted. 这种演变包括在C ++ 14中向count-only构造函数添加一个可选的allocator参数,这是(我假设)错误地省略了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM