On 2015-11-11 03:52,
len...@gmail.com wrote:
> I didn't find a topic like this, so here it is. Something like this would
> be nice:
>
> template <class... Args>
> vector(size_type n,Args&&... args);
>
> 1) It could be faster than vector(n,value_type(args)) if value_type's copy
> constructor is more expensive than its constructor with "args" parameters
> (I don't know a good example though).
Interesting idea. And since vector(n, value_type(...)) invokes
value_type's copy ctor, it is in fact a subset of the proposed syntax,
i.e. that form could be deprecated / removed in favor of the proposed
"emplace" version.
> 2) If value_type's different constructors are not deterministic then maybe
> it's desirable to call the constructor multiple times instead of copying
> one constructed element. One example if value_type has a constructor taking
> a random generator as an argument.
Hmm... for this case, you are thinking that one of the arguments is e.g.
a functor? And that the value_type copy ctor copies existing data, while
the ctor taking the functor uses the functor (which isn't stored) to
generate a value? Interesting notion...
> Workarounds:
> 2) Construct an empty vector, use reserve then emplace_back newly
> constructed elements one by one using value_type's constructor of choice.
> I'm not sure if this would be equally efficient.
Seems like it should be at least comparably efficient; I can't think
offhand what vector's sized constructor would do differently that would
make it more efficient. (Assuming that a default-constructed data has
SOO or otherwise doesn't allocate storage that gets thrown out by the
reserve().)
--
Matthew